2013-03-26 66 views
8

我發現並嘗試了許多在互聯網上的解決方案,它應該允許我禁用我的WPF列表框的懸停效果,但它們都不適用於我。該截圖顯示懸停效果我想隱藏或擺脫:WPF列表框關閉懸停效果

hover effect

這是XAML代碼(簡化版)我目前有:

<ListBox ItemsSource="{Binding Logs}" Grid.Column="1" Width="800" Height="100" > 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="ListBoxItem"> 
      <Style.Triggers> 
       <Trigger Property="Control.IsMouseOver" Value="True"> 
        <Setter Property="Control.Background" Value="Transparent" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </ListBox.ItemContainerStyle> 
</ListBox> 

然而,它似乎不適合我的理由或另一個。我的父母ListBox(背景中的那個)還是其他控件可以重寫它的孩子風格? (我已經試圖覆蓋父母的風格)

任何幫助將不勝感激。 :(

+3

我剛剛創建了一個新的WPF應用程序只是一個列表框觸發器,我沒有默認情況下懸停效果 – Tomtom 2013-03-26 08:55:55

+0

這很讓人困惑,我已經在Windows 8上測試了它(在屏幕截圖中看到),在Windows 7上,懸停效果顯示在一個醜陋的藍色中......我會嘗試創建一個新的wpf-application只需要一個列表框就可以在一分鐘內驗證它是否有所作爲 – beta 2013-03-26 09:01:39

+0

是的你是對的,默認情況下,ListBox甚至沒有懸停效果。所以我猜這是因爲我們爲所有子控件設置了全局樣式。有什麼辦法可以重寫這種風格嗎?我目前正在嘗試開始使用 beta 2013-03-26 09:14:06

回答

13

修改ListBoxItem的默認樣式

<Style TargetType="{x:Type ListBoxItem}"> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
     <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
     <Setter Property="Padding" Value="2,0,0,0"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true"> 
         <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsSelected" Value="true"> 
          <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> 
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/> 
         </Trigger> 
         <MultiTrigger> 
          <MultiTrigger.Conditions> 
           <Condition Property="IsSelected" Value="true"/> 
           <Condition Property="Selector.IsSelectionActive" Value="false"/> 
          </MultiTrigger.Conditions> 
          <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/> 
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}}"/> 
         </MultiTrigger> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

只需添加

<Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="Background" Value="Transparent"/> 
         </Trigger> 
+5

爲什麼不是'