How to set background color based on bool property in WPF

33,237

Solution 1

I ran into a few troubles trying to do this, ended up like so

<ListBox ...>
  <ListBox.ItemTemplate>
    <DataTemplate>
      <Border x:Name="BGBorder">
         <!-- --> 
      </Border>
      <DataTemplate.Triggers>
        <DataTrigger 
          Binding="{Binding Path=DataContext.IsAborted, RelativeSource={RelativeSource TemplatedParent}}" 
          Value="True">
          <Setter Property="Background" TargetName="BGBorder" Value="Red">
          </Setter>
        </DataTrigger>
      </DataTemplate.Triggers>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

There may be alternatives, but once I had it working, I stopped looking :)

Solution 2

I haven't tested this yet so it might need some tweaks but you're going to want to trigger off the value to set your background color.

<DataTemplate.Triggers>
    <Trigger Property="IsInternalNote" Value="True">
        <Setter Property="Background" Value="Red" />
    </Trigger>
</DataTemplate.Triggers>
Share:
33,237

Related videos on Youtube

Russ
Author by

Russ

As a key member of any technology architecture team I do what it takes to accomplish technical and business goals with quality, efficiency, and accountability. I specialize in providing technology leadership that inspires while bringing new ideas, and technologies that result in high performance, high availability enterprise products. My primary goal has always been to improve efficiency and productivity for the company through direct contributions as well as the technical development and mentorship of the team working with me. I bridge the Communication gap with leadership, team leads, project managers, and internal partners to guarantee reliable and regular software delivery. While simultaneously bringing a broad level of expertise in both agile software development and system integration, as well as a big picture approach to development with organizational goals and measurable business results.

Updated on December 13, 2020

Comments

  • Russ
    Russ over 3 years

    I want to set the backgroun color for a GridViewColumn that is databound inside of a listview in WPF. I'm not sure how to ask this question being fairly new to WPF, otherwise I wouldn't have bothered all of you.

    I want to change the background color of the whole row, based on a bool flag in my databound object.

    In this case, I have, well, a "CaseDetail" object, that when there are internal notes "IsInternalNote" I want the color of the row to change.

    How can I pull this off in WPF?

    What I have now, ( very simple ), which does NOT change the color.

    <ListView ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True"  >
                <ListView.View>
                    <GridView>
                        <GridViewColumn DisplayMemberBinding="{Binding Date, StringFormat=MMM dd\, yyyy h:mm tt}" Header="Date" Width="Auto" />
                        <GridViewColumn DisplayMemberBinding="{Binding SubmittedBy}" Header="Submitted By" Width="Auto" />
                        <GridViewColumn Width="Auto" Header="Description" x:Name="colDesc">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>                              
                                    <ScrollViewer MaxHeight="80" Width="300">
                                        <StackPanel Orientation="Vertical">
                                            <TextBlock Text="{Binding Description}"  TextWrapping="Wrap"   />
                                            <TextBlock Text="{Binding File.FileName}" TextWrapping="Wrap"  />
                                        </StackPanel>
                                    </ScrollViewer>                             
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>                    
                    </GridView>
                </ListView.View>
            </ListView>