2016-11-28 151 views
0

我在互联网上搜索了一段时间,在这里找到我的问题的解决方案,但我不知道为什么我试过的所有解决方案都不起作用。我必须首先说我在WPF中是全新的,所以也许这可能是真正的问题,但我解释了我想实现的目标。WPF listview - 在选择和选定项目上的透明背景

我建立一个WPF控件,我会在另一个应用程序导入(这是不是一个WPF应用程序,但我可以添加WPF控件),这种控制是一个简单的列表视图至极具有这些特征: - 透明背景 - 边界圆角

我这样做是因为我在其他应用程序,我有一个黑暗的背景色调,我想这种控制是透明的,所以你可以看到背景

搜索互联网上,我发现我需要什么,但我缺少的是当鼠标悬停在物品上时更改选定的物品背景前景和相同的东西(希望这是明确的)

这是我使用的测试代码:

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

此背景和圆角边框做工精细,但项目一直被光高亮颜色和白色文字,whick对我不好。我想要的是有一个透明的边框,而不是高亮的项目,鼠标悬停和选定的项目。

另一件事是,因为我添加了模板,标题不再显示,但我也想拥有标题。

任何人都可以帮助我做到这一点? 很多感谢

编辑: 这是发生了什么 Image的图像。我想有一个透明的边框,而不是

编辑2: 背景是一个图像。我试着评论“ListView.View”部分,我得到了我想要的结果,如image所示,但是项目不显示。我需要通过代码添加项目

+0

你可以发布你有什么和你需要什么图像? – Prajwal

+0

设置窗口的背景为透明。这就是为什么你正在查看窗口的背景颜色。 – Prajwal

+0

@Prajwal正如我在帖子中所说,背景是一个图像。我试过,但似乎我不能添加更多的背景一个属性。似乎Listview.View部分导致问题 – Hydra

回答

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