2011-09-27 88 views
2

我有一些簡單的ListBox對象。 當鼠標在某個對象上時,對象背景顏色發生變化。如何禁用項目上的鼠標項目時ListBox中的顏色​​更改?

是否有可能避免這種顏色變化與禁用對象? (此對象包含必須啓用的按鈕)

感謝您的任何幫助。

+0

我以爲你不想在鼠標懸停時突出顯示一個列表框項目容器。但是,「對象」也許是一種不同的控制方式,所以關於它的類型的信息將會很重要。 – vorrtex

回答

3

您可以從ListBoxItem模板中刪除視覺狀態動畫。 默認模板可以找到here

沒有鼠標懸停背景的風格看起來如此:

<Style x:Key="IgnoreMouseOverItem" TargetType="ListBoxItem"> 
    <Setter Property="Padding" Value="3" /> 
    <Setter Property="HorizontalContentAlignment" Value="Left" /> 
    <Setter Property="VerticalContentAlignment" Value="Top" /> 
    <Setter Property="Background" Value="Transparent" /> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Setter Property="TabNavigation" Value="Local" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ListBoxItem"> 
       <Grid Background="{TemplateBinding Background}"> 
        <vsm:VisualStateManager.VisualStateGroups> 
         <vsm:VisualStateGroup x:Name="CommonStates"> 
          <vsm:VisualState x:Name="Normal" /> 
          <vsm:VisualState x:Name="MouseOver"> 
           <!--<Storyboard> 
            <DoubleAnimation Storyboard.TargetName="fillColor" Storyboard.TargetProperty="Opacity" Duration="0" To=".35"/> 
           </Storyboard>--> 
          </vsm:VisualState> 
          <vsm:VisualState x:Name="Disabled"> 
           <Storyboard> 
            <DoubleAnimation Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="Opacity" Duration="0" To=".55" /> 
           </Storyboard> 
          </vsm:VisualState> 
         </vsm:VisualStateGroup> 
         <vsm:VisualStateGroup x:Name="SelectionStates"> 
          <vsm:VisualState x:Name="Unselected" /> 
          <vsm:VisualState x:Name="Selected"> 
           <Storyboard> 
            <DoubleAnimation Storyboard.TargetName="fillColor2" Storyboard.TargetProperty="Opacity" Duration="0" To=".75"/> 
           </Storyboard> 
          </vsm:VisualState> 
         </vsm:VisualStateGroup> 
         <vsm:VisualStateGroup x:Name="FocusStates"> 
          <vsm:VisualState x:Name="Focused"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Visibility" Duration="0"> 
             <DiscreteObjectKeyFrame KeyTime="0"> 
              <DiscreteObjectKeyFrame.Value> 
               <Visibility>Visible</Visibility> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </vsm:VisualState> 
          <vsm:VisualState x:Name="Unfocused"/> 
         </vsm:VisualStateGroup> 
        </vsm:VisualStateManager.VisualStateGroups> 
        <Rectangle x:Name="fillColor" Opacity="0" Fill="#FFBADDE9" IsHitTestVisible="False" RadiusX="1" RadiusY="1"/> 
        <Rectangle x:Name="fillColor2" Opacity="0" Fill="#FFBADDE9" IsHitTestVisible="False" RadiusX="1" RadiusY="1"/> 
        <ContentPresenter 
          x:Name="contentPresenter" 
          Content="{TemplateBinding Content}" 
          ContentTemplate="{TemplateBinding ContentTemplate}" 
          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
          Margin="{TemplateBinding Padding}"/> 
        <Rectangle x:Name="FocusVisualElement" Stroke="#FF6DBDD1" StrokeThickness="1" Visibility="Collapsed" RadiusX="1" RadiusY="1" /> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

,然後應用新樣式到ListBox

<ListBox ItemContainerStyle="{StaticResource IgnoreMouseOverItem}" /> 
+0

只是缺少標籤 ;-) –

+0

@ user2056563你究竟想要什麼,爲什麼你發佈一個鏈接到我們現在的同一頁面? – vorrtex

+0

@vorrtex我很抱歉,我將刪除該評論,我需要你的幫助http://stackoverflow.com/questions/22578128/list-box-with-multiple-data-template-style-for-selected-item – user2056563

1

如果你只是想避免突出顯示,你可以使用ItemsControl的。

相關問題