是否可以将 WPF 边框的 BorderBrush 绑定到 ViewModel class 中的变量?
Is it possible to bind BorderBrush of a WPF Border to a variable inside ViewModel class?
开始编写我的自定义 WPF window,我希望在 VM class 中包含颜色以及 ResizeThickness 和所有这些东西,以便在设置中编辑配色方案。
这两个都有效:
... BorderBrush = "Red" >
... BorderBrush = "{StaticResource [SolidColorBrush from xaml dict]}" >
但是... BorderBrush = "{Binding [SolidColorBrush from VM class]" >
什么也没做。
显然,DataContext 在 window 构造函数的代码中设置为所述 VM class。
我的第二个好主意是只编辑 xaml 并要求重新启动。
像这样在您的视图模型中定义画笔:
public System.Windows.Media.Brush MyBrush { get; set; }
...然后像这样使用它:
BorderBrush="{Binding MyBrush}"
您可以将 Brush 添加到容器对象的 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" />
开始编写我的自定义 WPF window,我希望在 VM class 中包含颜色以及 ResizeThickness 和所有这些东西,以便在设置中编辑配色方案。
这两个都有效:
... BorderBrush = "Red" >
... BorderBrush = "{StaticResource [SolidColorBrush from xaml dict]}" >
但是... BorderBrush = "{Binding [SolidColorBrush from VM class]" >
什么也没做。
显然,DataContext 在 window 构造函数的代码中设置为所述 VM class。
我的第二个好主意是只编辑 xaml 并要求重新启动。
像这样在您的视图模型中定义画笔:
public System.Windows.Media.Brush MyBrush { get; set; }
...然后像这样使用它:
BorderBrush="{Binding MyBrush}"
您可以将 Brush 添加到容器对象的 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" />