gpt4 book ai didi

c# - 为什么此绑定(bind)不能通过 XAML 工作,而是通过代码工作?

转载 作者:太空宇宙 更新时间:2023-11-03 18:18:14 25 4
gpt4 key购买 nike

我正在尝试绑定(bind)到静态类上的静态属性,
此属性包含从文件反序列化的设置。

它从不与以下 XAML 一起使用:

    <Window.Resources>
<ObjectDataProvider x:Key="wrapper" ObjectType="{x:Type Application:Wrapper}"/>
</Window.Resources>

<ScrollViewer x:Name="scrollViewer" ScrollViewer.VerticalScrollBarVisibility="Auto"DataContext="{Binding Source={StaticResource wrapper}, UpdateSourceTrigger=PropertyChanged}">

<ComboBox x:Name="comboboxThemes"
SelectedIndex="0"
SelectionChanged="ComboBoxThemesSelectionChanged"
Grid.Column="1"
Grid.Row="8"
Margin="4,3" ItemsSource="{Binding Settings.Themes, Mode=OneWay}" SelectedValue="{Binding Settings.LastTheme, Mode=TwoWay}" />

但是它确实可以通过代码工作:
comboboxThemes.ItemsSource = Settings.Themes;

任何的想法 ?

谢谢 :-)

最佳答案

您的代码隐藏不执行绑定(bind),它直接将源分配给 ComboBox ...

如果你想在 XAML 中做同样的事情,你根本不需要绑定(bind),你只需要 StaticExtension标记扩展:

ItemsSource="{x:Static local:Settings.Themes}"

(其中 local 是包含 Settings 类的命名空间的 xmlns 映射)

关于c# - 为什么此绑定(bind)不能通过 XAML 工作,而是通过代码工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3002131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com