开发者

Dynamic highlight color in ListView

开发者 https://www.devze.com 2023-04-06 10:00 出处:网络
How can I make background highlight color depe开发者_Python百科ndent on some property of ListViewItem?This is an issue that people often ask for. Actually, for some reasons, when an item is selected i

How can I make background highlight color depe开发者_Python百科ndent on some property of ListViewItem?


This is an issue that people often ask for. Actually, for some reasons, when an item is selected in a ListView or ListBox, the Background color is not the one that is changed. It is a bit more tricky. In fact you need to override the value of the static color resources to which the item template is bound. So to change the higlighting colors of the items you have to do like this:

<ListView>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Orange"/>
            </Style.Resources>
        </Style>
    </ListView.ItemContainerStyle>
</List>

Here is a more developed explanation: Trigger for ListBoxItem


EDIT:

For changing selected background, you will have to override the ListViewItem's Template.

See this... http://msdn.microsoft.com/en-us/library/ms788717(v=vs.90).aspx.

Replace the {StaticResource SelectedBackgroundBrush} with your preferred background brush in the template.


To change backgrounds based on ANY other property that the control template does not rely upon, You can use triggers ...

 <ListView ...>
  <ListView.Resources>
   <Style TargetType="{x:Type ListViewItem}">
     <Style.Triggers>
       <Trigger Property="SomeListViewItemProperty" Value="Value1">
         <Setter Property="Background" Value="Red" />
       </Trigger>
       <Trigger Property="SomeListViewItemProperty" Value="Value2">
         <Setter Property="Background" Value="Yellow" />
       </Trigger>
     </Style.Triggers>
   </Style>
  <ListView.Resources>
 </ListView>

I hope this answers your question.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号