Jiří Batulka
Jiří Batulka

Reputation: 91

Changing ListViewItem background colour while using Material Design

I'm fairly new to WPF and I've run into a problem with my app design. I have UserControl with ListView and I need to change background colour of ListViewItems dynamically. When I add the following code into my ListView everything works fine.

<ListView.Resources>
    <Style TargetType="{x:Type ListViewItem}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Status}" Value="NotStarted">
                <Setter Property="Background" Value="White" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Status}" Value="Fail">
                <Setter Property="Background" Value="#FF6666" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Status}" Value="Success">
                <Setter Property="Background" Value="#80FF80" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ListView.Resources>

The problem is that when I add Material Design into my App.xaml file, the colours stop to change.

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Any ideas how to solve this?

Upvotes: 2

Views: 898

Answers (2)

HelloWorld
HelloWorld

Reputation: 31

 <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem" BasedOn="{StaticResource MaterialDesignGridViewItem}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding FirstName}" Value="John">
                        <Setter Property="Background" Value="Red" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListView.ItemContainerStyle>

Upvotes: 3

Jiř&#237; Batulka
Jiř&#237; Batulka

Reputation: 91

Correct answer in the comments.

Upvotes: 2

Related Questions