我正在尝试绑定(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/