2016-11-28 148 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