How to change a WPF window backgroud with a data trigger?

nportelli picture nportelli · Oct 20, 2009 · Viewed 17.7k times · Source

I want to change the background color of our apps main window when a property changes. We have a business date that can be changed and I want to change the window background when it has changed from expected. I've set up a property to tell this. But can I set a style datatrigger on a window that changes itself? Or would I need to do this in the app.xaml?

Answer

nportelli picture nportelli · Oct 21, 2009

I ended up kind of doing what Drew suggested. Except I didn't use a Dependency Property.

<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 is a property on my Viewmodel that gets set by a service. I'm not sure why this was so hard.