首页 文章

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

提问于
浏览
0

我在互联网上搜索了一段时间,在这里找到解决我的问题的方法,但我不知道为什么我尝试的所有解决方案都不起作用 . 我首先要说的是,我在WPF中是全新的,所以也许这可能是真正的问题,但我解释了我想要实现的目标 .

我正在构建一个WPF控件,我将导入另一个应用程序(这不是WPF应用程序,但我可以添加WPF控件),这个控件是一个简单的列表视图,具有以下功能: - 透明背景 - 带圆角的边框

我这样做是因为在我的其他应用程序中我有深色背景和阴影,我希望这个控件是透明的,所以你可以看到背景

在互联网上搜索我找到了我需要的东西,但我缺少的是当鼠标悬停在项目上时更改所选项目background-foreground和同样的事情(希望它清楚)

这是我用于测试的代码:

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

这适用于背景和圆角边框,但项目仍然用浅色和白色文本突出显示,这对我来说不好 . 对于鼠标悬停和所选项目,我想拥有的是透明边框而不是突出显示的项目 .

另一件事是,因为我添加了模板, Headers 不再显示,但我也想有 Headers .

任何人都可以帮我这样做吗?提前谢谢了

编辑:这是Image发生的事情的图像 . 我希望有一个透明的边框

编辑2:背景是图像 . 我试着评论"ListView.View"部分,我有想要的结果,如image所示,但不显示项目 . 我需要按代码添加项目

1 回答

  • 0
    <Window.Background>
            <ImageBrush ImageSource="C:\\BkGd.png"></ImageBrush> 
    </Window.Background>
    

    这,实际上是在设置背景 .

    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                                 Color="Transparent"/>
    </Style.Resources>
    

    将所选项目背景设置为透明 . 因此,你正在获得窗口的背景 . 但是,由于那个蓝色的亮点,你没有看到它 . 为此,您需要覆盖默认的控件样式 .

    这是怎么做的 .

    Overriding Control Styles

相关问题