Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 476
nportelli Avatar asked Dec 23 '22 08:12

nportelli


1 Answers

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.

like image 191
nportelli Avatar answered Dec 27 '22 22:12

nportelli