[英]WPF listview - Transparent background on selection and selected items
i searched for some time on internet and here to find a solution on my problem, but i don't know why all the solutions i tried doesn't work. 我在互联网上搜索了一段时间,然后在这里找到我的问题的解决方案,但是我不知道为什么我尝试过的所有解决方案都不起作用。 I have to say first of all that i'm totally new in WPF so maybe this can be the real problem, but i explain what i want to achieve. 首先,我必须说我是WPF的新手,所以也许这可能是真正的问题,但我解释了我想要实现的目标。
I am building a WPF control i will import in another application (which is not a WPF application, but i can add WPF controls), this control is a simple listview wich have these features: - transparent background - border with rounded corners 我正在构建一个WPF控件,我将导入另一个应用程序(这不是WPF应用程序,但是我可以添加WPF控件),该控件是具有以下功能的简单列表视图:-透明背景-圆角边框
I am doing this because in my other application i have a dark background with shades and i want this control to be transparent so you can see the background 我这样做是因为在我的其他应用程序中,我有一个带有阴影的深色背景,并且我希望此控件是透明的,以便您可以看到背景
Searching on internet i found what i needed, but what i am missing is to change the selected item background-foreground and the same thing when the mouse is over the item (hope it's clear) 在互联网上搜索时,我发现了我所需要的东西,但是我所缺少的是当鼠标悬停在该项目上时更改所选项目的背景前景和相同的东西(希望很清楚)
This is the code i am using for tests: 这是我用于测试的代码:
<Window x:Class="TestWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="501" Width="792">
<Window.Background>
<ImageBrush ImageSource="C:\\BkGd.png"></ImageBrush>
</Window.Background>
<Grid>
<ListView Margin="10" Name="lvUsers" Background="Transparent" Foreground="White" FontStyle="Normal" FontWeight="Normal" FontFamily="Segoe UI" >
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Resources>
<!-- Foreground for Selected ListViewItem -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
Color="Black"/>
<!-- Background for Selected ListViewItem -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Transparent"/>
</Style.Resources>
</Style>
</ListView.ItemContainerStyle>
<ListView.Template>
<ControlTemplate TargetType="{x:Type ListView}">
<Border CornerRadius="3" BorderThickness="1" BorderBrush="DarkGray">
<ScrollViewer>
<ItemsPresenter />
</ScrollViewer>
</Border>
</ControlTemplate>
</ListView.Template>
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
<GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
This works fine for the background and the rounded corner border, but the items keeps being highlighted with light colors and white text, whick is not good for me. 这对于背景和圆角边框来说效果很好,但是项目始终以浅色和白色文本突出显示,抽动对我不利。 What i would like to have is to have a transparent border instead of the highlighted item, both for mouse hover and selected item. 我想拥有的是鼠标悬停和选定项目都具有透明边框而不是突出显示的项目。
One other thing is that since i added the template, header are not shown anymore but i'd like to have header too. 另一件事是,由于我添加了模板,因此不再显示标头,但我也希望有标头。
Can anyone help me doing this? 谁能帮我做到这一点? Many thanks in advance 提前谢谢了
EDIT: this is an image of what happens Image . 编辑:这是发生的图像的图像 。 I would like to have a transparent border instead 我想有一个透明的边框
EDIT 2: Background is an image. 编辑2:背景是图像。 I tried commenting the "ListView.View" part and i have the result i want as shown in this image , but items are not displayed. 我尝试评论“ ListView.View”部分,并且得到了想要的结果,如该图所示,但未显示项目。 I need to add items by code 我需要通过代码添加项目
<Window.Background>
<ImageBrush ImageSource="C:\\BkGd.png"></ImageBrush>
</Window.Background>
This, is actually setting up the background. 这实际上是在设置背景。
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Transparent"/>
</Style.Resources>
is setting the selected item background to transparent. 将所选项目的背景设置为透明。 Hence, you are getting background of window. 因此,您正在获取窗口背景。 But, you are not seeing that because of that blue highlight. 但是,由于蓝色突出显示,您看不到它。 For that, you need to override default Control Styles. 为此,您需要覆盖默认的控件样式。
Here's how to do that. 这是这样做的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.