How to override ListViewItem style?

我与影子孤独终老i 提交于 2019-12-11 07:33:49

问题


I'm trying to override the ListViewItem style using this code:

  <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource MaterialDesignListViewItem}">
            <EventSetter Event="PreviewMouseLeftButtonDown"
                         Handler="ListViewItem_PreviewMouseLeftButtonDown" />

        </Style>
    </ListView.ItemContainerStyle>

unfortunately seems that MaterialDesignListViewItem isn't included in the MaterialDesignInXaml so I got the StaticResource underlined for not found.

My goal is change the color of selection when the mouse is over an item.

Thanks for the attention.


回答1:


So there are a few things going on here.

In MDIX there are two styles that are used for ListViews. The grouped style, MaterialDesignGridViewItem and the normal MaterialDesignListBoxItem. To override either of these styles you will need to manually bring in the appropriate resource dictionary (MaterialDesignTheme.ListView.xaml and MaterialDesignTheme.ListBox.xaml respectively).

I think you are looking for something like this:

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem" BasedOn="{StaticResource MaterialDesignListBoxItem}">
        <Style.Resources>
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.ListBox.xaml" />
        </Style.Resources>
        <EventSetter Event="PreviewMouseLeftButtonDown"
                     Handler="ListViewItem_PreviewMouseLeftButtonDown" />
    </Style>
</ListView.ItemContainerStyle>


来源:https://stackoverflow.com/questions/48035365/how-to-override-listviewitem-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!