我想在属性更改时更改我们的应用程序主窗口的背景颜色。我们有一个可以更改的营业日期,我想在它与预期不同时更改窗口背景。我已经设置了一个属性来说明这一点。但是我可以在自己改变的窗口上设置一个样式数据触发器吗?或者我需要在 app.xaml 中执行此操作吗?

最佳答案

我最终按照德鲁的建议做了。除了我没有使用依赖属性。

<Window.Resources>
   <SolidColorBrush x:Key="windowBGBrush" Color="Green"/>
   <SolidColorBrush x:Key="windowBGBrushBusinessDateChanged" Color="Red"/>
</Window.Resources>
<Window.Style >
   <Style TargetType="{x:Type Window}">
      <Setter Property="Background" Value="{DynamicResource windowBGBrush}"/>
      <Style.Triggers>
         <DataTrigger Binding="{Binding IsBusinessDateChanged}" Value="true">
            <Setter Property="Background" Value="{DynamicResource windowBGBrushBusinessDateChanged}"/>
         </DataTrigger>
      </Style.Triggers>
   </Style>
</Window.Style>
IsBusinessDateChanged 是我的 Viewmodel 上的一个属性,由服务设置。我不知道为什么这这么难。

关于wpf - 如何使用数据触发器更改 WPF 窗口背景?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1595151/

10-17 01:22
查看更多