是否可以将 WPF 边框的边框刷绑定到 ViewModel 类中的变量



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

这两种方法都有效:

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

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

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

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

在视图模型中定义画笔,如下所示:

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

。然后像这样使用它:

BorderBrush="{Binding MyBrush}"

您可以将画笔添加到容器对象的资源字典中,并将其重用于所有控件...

<!-- 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" />

最新更新