German Shepherd
German Shepherd

Reputation: 113

ListBox WPF: change foreground color of SelectedItem and keep Material Design?

How can i change the forground color of the SelectedItem without removing the Style from Material Design ?

This Works but will remove the Style from Material Design:

<ListBox TextElement.Foreground="Black">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel IsItemsHost="True" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True" >
                            <Setter Property="FontWeight" Value="Bold" />
                            <Setter Property="Background" Value="Transparent" />
                            <Setter Property="Foreground" Value="White" />
                        </Trigger>
                    </Style.Triggers>
                    <Style.Resources>
                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                    </Style.Resources>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>

How can i add the Style insted of Replacing it ?

Upvotes: 2

Views: 2624

Answers (1)

Sven Bardos
Sven Bardos

Reputation: 872

Change Style to BasedOn="{StaticResource MaterialDesignListBoxItem}"

 <ListBox.ItemContainerStyle>
       <Style TargetType="ListBoxItem" BasedOn="{StaticResource MaterialDesignListBoxItem}">
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True" >
                            <Setter Property="FontWeight" Value="Bold" />
                            <Setter Property="Background" Value="Transparent" />
                            <Setter Property="Foreground" Value="White" />                               
                        </Trigger>
                    </Style.Triggers>
                    <Style.Resources>
                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                    </Style.Resources>
      </Style>
 </ListBox.ItemContainerStyle>

Upvotes: 5

Related Questions