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

标签 c# wpf binding static properties

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

相关文章:

c# - 使用插件将字段设置为只读

c# - pjsip c#,在单独线程上应答传入调用时出现 System.AccessViolationException

WPF 统一网格布局

c# - 将 SQL Server 异常传递给 .net

c# - 检查文件/文件夹访问权限

WPF——为什么这个 EventTrigger 不起作用?

jquery - 销毁 jquery 函数和我的对象之间的绑定(bind)?

c# - 绑定(bind)到 "IsExpanded"不适用于 TreeView 中的根节点

WPF:在样式和/或数据模板 (MVVM) 中绑定(bind)命令?

c# - 使用 .NET Core 2 的 Azure WebJobs 失败并显示 "Invalid storage account"