简体   繁体   English

WPF列表视图-选择和选定项目的透明背景

[英]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. 这是这样做的方法。

Overriding Control Styles 覆盖控件样式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WPF透明ListView背景与非透明ListViewItems - WPF transparent ListView background with non transparent ListViewItems 树视图中的 WPF ListView 选择我需要在选择更改事件中获取所有选定的 ListView 项目(按住 CTRL 键时) - WPF ListView Selection inside a treeview I Need to get all the selected ListView Items(when Holding CTRL key) in selection changed event WPF将ListView选定的项目传递给另一个ListView - WPF pass ListView selected items to another ListView WPF筛选ListView删除选定的项目 - WPF filtering ListView removes Selected Items 如何在 WPF DataGrid 中用透明选择颜色勾勒出所选行? - How to outline the selected row with transparent selection color in WPF DataGrid? 在后台工作程序中循环浏览选定的列表视图项 - Loop through selected listview items in background worker 修剪选定项目列表视图的选择区域WPF - trim down selection area of selected item listview wpf wpf绑定到选定的listview项并根据该选择更新另一个列表 - wpf bind to selected listview item and update another list based on that selection Windows 8 ListView(多项选择)取消选择所有期望最近选择的项目吗? - Windows 8 ListView (multiple selection) unselect all items expect latest selected? WPF透明用户控件背景 - WPF Transparent UserControl Background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM