AngryCarrot789/WPFDarkTheme

ListView wrong style when selected

Closed this issue · 5 comments

Hi,
I have the following code, and when an element is selected, it is the default Windows style.

<ListView Grid.Row="0" ItemsSource="{Binding Enemies}" 
          SelectionChanged="OnList_ListView_SelectionChanged"
          MouseDown="OnList_ListView_MouseDown">
    <ListView.Resources>
        <ContextMenu x:Key="ListViewContextMenu">
            <MenuItem Header="Edit" Click="OnEdit_Button_Click" />
            <Separator />
            <MenuItem Header="Delete" Click="OnDelete_Button_Click" />
        </ContextMenu>
    </ListView.Resources>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="ContextMenu" Value="{StaticResource ListViewContextMenu}"/>
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Header="Name" Width="430" />
            <GridViewColumn DisplayMemberBinding="{Binding Path=Health}" Header="Health" Width="110" />
            <GridViewColumn DisplayMemberBinding="{Binding Path=Speed}" Header="Speed" Width="110" />
            <GridViewColumn DisplayMemberBinding="{Binding Path=Damage}" Header="Damage" Width="110" />
        </GridView>
    </ListView.View>
</ListView>

image

I assume you already figured out, but it was because your ListView.ItemContainerStyle wasn't BasedOn the style the theme provides

I assume you already figured out, but it was because your ListView.ItemContainerStyle wasn't BasedOn the style the theme provides

Can you provide an example here?

OP's style that targets ListViewItem doesn't have a control template, and since there's no BasedOn statement it falls back to the WPF default. Putting BasedOn="{StaticResource {x:Type ListViewItem}}" would fill in the style data from this theme which includes the control template, background, etc

Thanks. That got me close as I didn't know where that BasedOn would go. I also wasn't sure whether the "style the theme provides" meant I should reference the style name (e.g., SoftDark) as the resource.

            <ListView.ItemContainerStyle>
              <Style TargetType="ListViewItem" BasedOn="{StaticResource {x:Type ListViewItem}}">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="Focusable" Value="false"/>
              </Style>
            </ListView.ItemContainerStyle>

worked for me.

Yeah that's pretty much all you have to do if you want to change some of the default properties without changing the actual template of the item. As soon as you want to add to the control template, then you'll have to copy from the Controls.xaml. BasedOn is pretty much just style inheritance; you just add or change what you, from the BasedOn one

By "style the theme provides" I mean there's only a single ListViewItem style for this entire "theme pack" i suppose you could call it, and that style uses the active theme colour dictionary to change the look. Not exactly a great system but it makes it easier to add new colour themes without changing the actual layout of controls.