列表视图选择颜色

发布于 2024-07-21 13:08:53 字数 2160 浏览 5 评论 0原文

我正在玩 wpf,我看到了以下文章: WPF ListView 非活动选择颜色

我想做类似的事情。 我想在选择列表视图项时在其周围放置边框,并且不想更改背景颜色。 我想要这个的原因是我想要一个颜色编码的列表视图,并且我仍然想在选择它时看到颜色,但我想知道它是通过它周围有边框来选择的。

有任何想法吗?

更新:

我尝试了下面的答案,它让我成功了一半,它确实在 listviewitem 周围放置了边框,但它覆盖了我的背景颜色。 我无法获得我尝试过的正确语法(注意 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>

然后我尝试了这个:

    <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>

两次尝试都只是将背景设置为白色(或透明,我不知道)。 我知道这只是语法,我希望在正确的方向上再次推动:)

I'm playing around with wpf and I saw the following article:
WPF ListView Inactive Selection Color

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.

Any ideas?

UPDATE:

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>

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵强ㄟ 2024-07-28 13:08:53

将 ListView 上的 ItemContainerStyle 更改为在选择项目时不更改背景但更改边框颜色的样式。 下面是一个例子:

  <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>

然后使用这样的样式:

<ListView ItemContainerStyle="{StaticResource MyListViewItemStyle}">
   ...
</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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文