列表视图选择颜色 [英] Listview selection color

查看:101
本文介绍了列表视图选择颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和WPF玩弄,我看到下面的文章:
<一href=\"http://stackoverflow.com/questions/382006/wpf-listview-inactive-selection-color\">http://stackoverflow.com/questions/382006/wpf-listview-inactive-selection-color

I'm playing around with wpf and I saw the following article: http://stackoverflow.com/questions/382006/wpf-listview-inactive-selection-color

我想要做类似的事情。我想被选中时,它把一个边框的一个ListViewItem的,我想不会改变背景色。我想这个的原因是我想要一个颜色codeD ListView和我仍然希望看到的颜色时,它的选择,但是我想知道它是由有它周围的边框它选中。

I want to do something similar. I want to put a border around an a listviewitem when it is selected and i want to not change the background color. The reason I want this is I want a color coded listview and I still want to see the color when it's selected, but i want to know it's selected by it having a border around it.

任何想法?

更新:

我试过下面的答案,它让我半路上,它把周围的边框ListViewItem的,但它会覆盖我的背景颜色。我不能得到正确的语法我尝试(注意支持算法FMP):

I tried the below answer and it got me half way, it does put a border around the listviewitem but it overrides my background color. I can't get the right syntax i tried(Notice the BasedOn):

    <Style x:Key="SourceListView" TargetType="{x:Type ListViewItem}">
        <Setter Property="Background" Value="{Binding SourceType, Converter={StaticResource SourceGroupConverter}}"/>
    </Style>

    <Style x:Key="MyListViewItemStyle" TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource SourceListView}" >
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Border
                             x:Name="Border"
                             BorderBrush="Transparent"
                             BorderThickness="1">
                        <GridViewRowPresenter Columns="{TemplateBinding GridView.ColumnCollection}" Content="{TemplateBinding Content}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter TargetName="Border" Property="BorderBrush" Value="Black"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我又试图这样的:

I then tried this:

    <Style x:Key="MyListViewItemStyle" TargetType="{x:Type ListViewItem}">
        <Setter Property="Background" Value="{Binding SourceType, Converter={StaticResource SourceGroupConverter}}"/>
        <Setter Property="Template">
            ...//Same as above
        </Setter>
    </Style>

尝试都只是将背景设置为白色(或透明的,我不知道)。我知道这只是语法和我最好的AP preciate在正确的方向轻移另一:)

Both attempts just set the background to white(or transparent I don't know). I know it's just syntax and I'd appreciate another nudge in the right direction :)

推荐答案

更改ItemContainerStyle上的ListView当选择一个项目,不更改背景样式,而是改变边框的颜色。下面是一个例子:

Change the ItemContainerStyle on the ListView to a style that doesn't change the background when an item is selected but instead changes the color of a border. Below is an example:

  <Style x:Key="MyListViewItemStyle" TargetType="{x:Type ListViewItem}">
     <Setter Property="Background" Value="{Binding SourceType, Converter={StaticResource SourceGroupConverter}}" />
     <Setter Property="Template">
        <Setter.Value>
           <ControlTemplate TargetType="{x:Type ListViewItem}">
              <Border
                 x:Name="Border"
                 BorderBrush="Transparent"
                 BorderThickness="1"
                 Background="{TemplateBinding Background}">
                 <GridViewRowPresenter Columns="{TemplateBinding GridView.ColumnCollection}" Content="{TemplateBinding Content}"/>
              </Border>
              <ControlTemplate.Triggers>
                 <Trigger Property="IsSelected" Value="true">
                    <Setter TargetName="Border" Property="BorderBrush" Value="Black"/>
                 </Trigger>
              </ControlTemplate.Triggers>
           </ControlTemplate>
        </Setter.Value>
     </Setter>
  </Style>

和再使用的样式是这样的:

And then use the style like this:

<ListView ItemContainerStyle="{StaticResource MyListViewItemStyle}">
   ...
</ListView>

这篇关于列表视图选择颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆