代码之家  ›  专栏  ›  技术社区  ›  Whazz

是否可以将WPF边界的BorderBrush绑定到ViewModel类中的变量?

  •  0
  • Whazz  · 技术社区  · 8 年前

    开始编写我的自定义WPF窗口,我希望在VM类中有颜色以及ResizeThickness和所有这些东西,以便在设置中可以编辑颜色方案。

    这两项工作:

    ... BorderBrush = "Red" >
    ... BorderBrush = "{StaticResource [SolidColorBrush from xaml dict]}" >
    

    但是 ... BorderBrush = "{Binding [SolidColorBrush from VM class]" > 什么都不做。

    显然,DataContext在窗口构造函数的代码中设置为上述VM类。

    我的第二个好主意是编辑xaml并请求重新启动。

    2 回复  |  直到 8 年前
        1
  •  1
  •   Jake Reece    8 年前

    在viewmodel中定义笔刷,如下所示:

    public System.Windows.Media.Brush MyBrush { get; set; }
    

    。。。然后像这样使用:

    BorderBrush="{Binding MyBrush}"
    
        2
  •  0
  •   wp78de    8 年前

    您可以将画笔添加到容器对象的ResourceDictionary中,并将其用于所有控件。。。

    <!-- Add the Brush as resource to the surrounding window -->
    <Window.Resources>
      <SolidColorBrush x:Key="controlBorderBrush" Color="Gray" />
    </Window.Resources>
    
    <TextBlock BorderBrush="{StaticResource controlBorderBrush}" Text="xyz" />