【问题标题】:Binding Settings based SolidColorBrush directly in XAML [duplicate]直接在 XAML 中绑定基于 SolidColorBrush 的设置 [重复]
【发布时间】:2018-01-03 13:12:49
【问题描述】:

给定一个Settings.settings 文件,其中包含一个或多个SolidColorBrush 条目和Application 范围,我如何将这些值绑定到XAML 样式模板?

例如,我将有一个名为MyAppColorSolidColorBrush#FF0091D2 作为值和一个自定义Button 样式模板,其中我希望Background 具有来自MyAppColor 的颜色。我怎样才能做到这一点?


我已经尝试将 namespace (?) 添加到 xaml 文件中,如下所示:

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:properties="clr-namespace:namespace.Properties"

然后尝试像这样使用properties

Background="{x:Static properties:Settings.Default.MyAppColor}"

但它说它不能有嵌套类型。

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    虽然Settings.Default 是静态的,但MyAppColor 属性不是。 (Settings.Default 实际上是一个 Singletone)。使用 {x:Static} 扩展来设置Binding.Source 并照常提供Binding.Path

    Background="{Binding Path=MyAppColor, Source={x:Static properties:Settings.Default}}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多