为什么这个绑定不能通过XAML工作,而是通过代码工作?

Ayb*_*ybe 1 c# wpf binding static properties

我试图绑定到静态类上的静态属性,此属性包含从文件反序列化的设置.

它永远不适用于以下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}"   />
Run Code Online (Sandbox Code Playgroud)

它确实通过代码工作:

comboboxThemes.ItemsSource = Settings.Themes;
Run Code Online (Sandbox Code Playgroud)

任何的想法 ?

谢谢 :-)

Tho*_*que 5

你的代码隐藏不执行绑定,它直接为ComboBox... 分配一个源代码.

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

ItemsSource="{x:Static local:Settings.Themes}"
Run Code Online (Sandbox Code Playgroud)

(local包含Settings该类的命名空间的xmlns映射在哪里)