thndrwrks
thndrwrks

Reputation: 1674

C# WPF DataTemplate set background color on property

I have a TreeView containing a bunch of these Verify classes. Ultimately I would like to change the background color of an item to Green if the class's Success property is true and to Red if the property is false.

public class Verify : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public string Name { get { return "Dummy Text"; } }

    private bool success;
    public bool Success
    {
        get { return success; }
        set { success = value; NotifyPropertyChanged(); }
    }

    public Verify()
    {
        Success = true; /* Test that the background changes color */
    }
}

Here is where I've gotten so far in my TreeView. To test I've created an ObservableCollection<Verify> VerifyWrite and added a couple entries to it. This is what my TreeView is being bound to. I expect all the entries in my TreeView to be Green because I've set the Success to true but the background is not set to anything.

        <TreeViewItem Header="Verify Write" IsExpanded="True" ItemsSource="{Binding VerifyWrite}">
            <TreeViewItem.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"/>

                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding Success}" Value="True">
                            <Setter Property="TreeViewItem.Background" Value="Green"/>
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </TreeViewItem.ItemTemplate>
        </TreeViewItem>

I am very new and very lost in this WPF thing.

Upvotes: 3

Views: 6171

Answers (1)

Gman
Gman

Reputation: 1876

Triggers inside templates apply only to elements inside templates. And for that you need to name those elements:

<DataTemplate>
    <Grid x:Name="ItemBackground">
        <TextBlock Text="{Binding Name}"/>
    </Grid>

    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding Success}" Value="True">
            <Setter TargetName="ItemBackground" Property="Background" Value="Green"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

But this approach would not allow you to color the whole item. For this, you should use ItemContainerStyle like this:

<TreeViewItem Header="Verify Write" IsExpanded="True" ItemsSource="{Binding VerifyWrite}">
    <TreeViewItem.ItemContainerStyle>
        <Style TargetType="TreeViewItem">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Success}" Value="True">
                    <Setter Property="Background" Value="Green"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TreeViewItem.ItemContainerStyle>
</TreeViewItem>

Upvotes: 3

Related Questions