2017-03-16 54 views
1

我已經設法防止在懸停和選擇時顯示ListBox項目背景,但是,當我右鍵單擊某個項目時,即使在ListBox再次對焦時顯示懸停時的背景(對於先前右鍵單擊的元素)。刪除右鍵單擊事件的ListBoxItem背景

我不能在ListBoxPreviewMouseDown中使用e.Handled = true,因爲這會打破我的右鍵單擊上下文菜單。通過中斷,我的意思是它可以防止菜單項對點擊作出反應。上下文菜單顯示正常,但無法調用項目點擊。

我錯過了什麼,還包括ListBoxItem背景不顯示右鍵單擊?

謝謝。

<!-- Style --> 
<Style TargetType="ListBoxItem" x:Key="MyListBoxItemStyle"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
       <Border x:Name="Bd" 
       Background="{TemplateBinding Background}" 
       BorderBrush="{TemplateBinding BorderBrush}" 
       BorderThickness="{TemplateBinding BorderThickness}" 
       Padding="{TemplateBinding Padding}" 
       SnapsToDevicePixels="True"> 
        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
           VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
           Content="{TemplateBinding Content}" 
           ContentStringFormat="{TemplateBinding ContentStringFormat}" 
           ContentTemplate="{TemplateBinding ContentTemplate}" 
           SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> 
       </Border> 
       <ControlTemplate.Triggers> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="IsMouseOver" Value="True" /> 
         </MultiTrigger.Conditions> 
         <Setter TargetName="Bd" Property="Background" Value="Transparent" /> 
         <Setter TargetName="Bd" Property="BorderBrush" Value="Transparent" /> 

        </MultiTrigger> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="Selector.IsSelectionActive" Value="False" /> 
          <Condition Property="IsSelected" Value="True" /> 
         </MultiTrigger.Conditions> 
         <Setter TargetName="Bd" Property="Background" Value="Transparent" /> 
         <Setter TargetName="Bd" Property="BorderBrush" Value="Transparent" /> 
        </MultiTrigger> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="Selector.IsSelectionActive" Value="True" /> 
          <Condition Property="IsSelected" Value="True" /> 
         </MultiTrigger.Conditions> 
         <Setter TargetName="Bd" Property="Background" Value="Transparent" /> 
         <Setter TargetName="Bd" Property="BorderBrush" Value="Transparent" /> 
        </MultiTrigger> 
        <Trigger Property="IsEnabled" Value="False"> 
         <Setter TargetName="Bd" Property="TextElement.Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<!-- Listbox --> 

<ListBox Name="ListBoxOne"> 
     <ListBox.Style> 
      <Style> 
       <Style.Triggers> 
        <Trigger Property="ListBox.IsMouseOver" Value="True"> 
         <Setter 
          Property="ListBox.ItemContainerStyle" 
          Value="{StaticResource MyListBoxItemStyle}"/> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </ListBox.Style> 
    </userControls:ListBox> 
+0

爲什麼不使用'ItemsControl' –

回答

3

就總是ListBoxItemContainerStyle屬性設置爲自定義樣式,即不設置它在Style內觸發:

<ListBox Name="ListBoxOne"> 
    <ListBox.Style> 
     <Style TargetType="ListBox"> 
      <Setter Property="ItemContainerStyle" Value="{StaticResource MyListBoxItemStyle}"/> 
     </Style> 
    </ListBox.Style> 
</ListBox> 
+0

那固定的問題完全。這當然是沒有看到樹木的情況。謝謝您的幫助! – PersuitOfPerfection