2012-07-02 95 views
3

我有一個ListView和Horizo​​ntal WrapPanel作爲其ItemsPanelTemplate。 我想擺脫選定項目的藍色背景。它僅在所選項目的左側可見。禁用選定列表視圖項目的藍色邊框

在SO上有很多類似的問題,我嘗試了很多解決方案,但都沒有工作。

這是我已經嘗試過:

<ListView.Resources> 
      <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"/> 
        <!--SelectedItem without focus--> 
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/> 
       </Style.Resources> 
      </Style> 
     </ListView.Resources> 



<ListView.ItemContainerStyle> 
      <Style TargetType="{x:Type ListViewItem}"> 
       <EventSetter Event="Control.MouseDoubleClick" Handler="HandleSelectedItemDoubleClick"/> 
       <Style.Triggers> 
        <Trigger Property="IsSelected" Value="true"> 
         <Setter Property="RenderTransform"> 
          <Setter.Value> 
           <ScaleTransform ScaleX="2" ScaleY="2" CenterX="12" CenterY="12" /> 
          </Setter.Value> 
         </Setter> 
         <Setter Property="Panel.ZIndex" Value="150"/> 
         <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
         <Setter Property="BorderBrush" Value="{x:Null}" /> 
         <Setter Property="Background" Value="{x:Null}" /> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </ListView.ItemContainerStyle> 
     <ListView.ItemsPanel> 
      <ItemsPanelTemplate> 
       <WrapPanel Orientation="Horizontal" Width="210" Margin="15" /> 
      </ItemsPanelTemplate> 
     </ListView.ItemsPanel> 

回答

8

你需要覆蓋SystemColors.HighlightBrushKeyListViewTransparent(或任何顏色你想)

我通常把這個在ListView.Resources所以它僅適用於特定ListView,並且不是所有的ListViews在我的應用

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

它非常接近你有什麼在你的代碼了,但你需要將其設置爲ListView.Resources,而不是ListViewItem.Resources

+0

它的工作,非常感謝。愛短的解決方案:)) –

0

這是爲我做:

<UserControl.Resources> 
    <DataTemplate x:Key="ItemTemplate"> 
     <StackPanel Orientation="Vertical" Background="Transparent" Opacity="1" Width="185" MaxWidth="185"> 
      <StackPanel Orientation="Horizontal"> 
      <TextBlock 
       Margin="0,0,10,0" 
          Background="Transparent" 
          Foreground="DarkGoldenrod" 
          FontSize="12" FontStyle="Italic" 
          Text="{Binding Path=EventTypeName, Mode=OneWay}" /> 
       <TextBlock 
        Background="Transparent" 
        Foreground="DarkGoldenrod" 
        FontSize="12" FontStyle="Italic" 
        Text="{Binding Path=AccountIdentity, Mode=OneWay}" /> 
      </StackPanel> 
      <TextBlock 
          Background="Transparent" 
          Foreground="DarkGoldenrod" MaxWidth="170" 
          FontSize="12" FontStyle="Italic" TextTrimming="WordEllipsis" ToolTip="{Binding Path=EventMessage,Mode=OneWay}" 
          Text="{Binding Path=EventMessage, Mode=OneWay}" /> 

      <TextBlock 
           Background="Transparent" 
           Foreground="Black" 
           FontSize="8" 
           Text="{Binding Path=EventLoggedOn, Mode=OneWay}" 
           TextTrimming="WordEllipsis" 
           ToolTip="{Binding Path=EventLoggedOn, Mode=OneWay}" 
           Margin="0,0,10,0" /> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock 
           Background="Transparent" 
           Foreground="Black" 
           FontSize="8" 
           Text="{Binding Path=QualifiedCreator, Mode=OneWay}" /> 
      </StackPanel> 
     </StackPanel> 
    </DataTemplate> 
    <DataTemplate x:Key="SelectedTemplate"> 
     <StackPanel Orientation="Vertical" Background="LightGray" Opacity="1" Width="185" MaxWidth="185"> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock 
        Margin="0,0,10,0" 
          Background="Transparent" 
          Foreground="Yellow" 
          FontSize="12" FontStyle="Italic" 
          Text="{Binding Path=EventTypeName, Mode=OneWay}" /> 
       <TextBlock 
          Background="Transparent" 
        Foreground="Yellow" 
        FontSize="12" FontStyle="Italic" 
        Text="{Binding Path=AccountIdentity, Mode=OneWay}" /> 
      </StackPanel> 
      <TextBlock 
          Background="Transparent" 
          Foreground="DarkGoldenrod" MaxWidth="170" 
          FontSize="12" FontStyle="Italic" TextTrimming="WordEllipsis" ToolTip="{Binding Path=EventMessage,Mode=OneWay}" 
          Text="{Binding Path=EventMessage, Mode=OneWay}" /> 

      <TextBlock 
           Background="Transparent" 
           Foreground="Black" 
           FontSize="8" 
           Text="{Binding Path=EventLoggedOn, Mode=OneWay}" 
           TextTrimming="WordEllipsis" 
           ToolTip="{Binding Path=EventLoggedOn, Mode=OneWay}" 
           Margin="0,0,10,0" /> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock 
           Background="Transparent" 
           Foreground="Black" 
           FontSize="8" 
           Text="{Binding Path=QualifiedCreator, Mode=OneWay}" /> 
      </StackPanel> 
     </StackPanel> 
    </DataTemplate> 
    <Style TargetType="ListViewItem" x:Key="ContainerStyle"> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="ContentTemplate" Value="{StaticResource ResourceKey=ItemTemplate}" /> 
     <Style.Triggers> 
      <Trigger Property="IsSelected" Value="True" > 
       <Setter Property="ContentTemplate" Value="{StaticResource ResourceKey=SelectedTemplate}" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</UserControl.Resources> 
0

要做到這一點,最簡單的辦法是將背景設置爲{X:空}選擇項目時,使用觸發器。

<ListView> 
    <ListView.ItemContainerStyle> 
     <Style TargetType="{x:Type ListViewItem}"> 
       <Style.Triggers> 
         <Trigger Property="IsSelected" 
             Value="True"> 
           <Setter Property="Background" 
               Value="{x:Null}" /> 
           <Setter Property="BorderBrush" 
               Value="{x:Null}" /> 
         </Trigger> 
       </Style.Triggers> 
     </Style> 
    </ListView.ItemContainerStyle> 
    ... 
</ListView> 
+0

這正是我在我的代碼中,藍色的東西仍然存在... –

+0

在這種情況下,您是否嘗試過使用Snoop或Wpf Inspector來查看突出顯示的項目並找出確切哪個元素已被突出顯示?這顯然不是ListViewItem,但其他的東西正在受到影響。 –