2014-04-09 88 views
0

我創建了自定義列表框並基於布爾我想禁用鼠標懸停和鼠標選擇少數項目。我使用Style類並在自定義列表框的代碼中設置樣式。我的代碼如下:禁用鼠標懸停和選擇自定義列表中的選定項目

public class DragDropListBox : ListBox 
{ 
    public DragDropListBox() 
    { 
     Style itemContainerStyle  = new Style(typeof(ListBoxItem)); 
     itemContainerStyle.Setters.Add(new EventSetter(ListBoxItem.DropEvent, new DragEventHandler(ListBoxItemDropHandler))); 
     this.ItemContainerStyle   = itemContainerStyle; 
    }   
} 

現在我已經設置一個事件引領者丟棄事件和正常工作。如何設置樣式來禁用基於標誌的項目的鼠標懸停和鼠標選擇效果?我發現的大部分代碼都在XAML中。但我需要我的自定義列表框的代碼。任何幫助,將不勝感激。

到目前爲止,這種風格是對我,而且什麼工作及其在XAML我怎麼把它轉換C#代碼,特別是可視狀態和故事板..

<Style x:Key="ListBoxItemStyleTransparentSelect" TargetType="ListBoxItem"> 
     <Setter Property="Background" Value="Transparent"/> 
     <EventSetter Event="Drop" Handler="listbox1_Drop"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ListBoxItem"> 
        <Grid Background="{TemplateBinding Background}"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal"/> 
           <VisualState x:Name="MouseOver"> 
            <Storyboard> 
             <DoubleAnimation Duration="0" To=".35" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="fillColor"/> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <DoubleAnimation Duration="0" To=".55" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="contentPresenter"/> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="SelectionStates"> 
           <VisualState x:Name="Unselected"/> 
           <VisualState x:Name="Selected"/> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="FocusStates"> 
           <VisualState x:Name="Focused"/> 
           <VisualState x:Name="Unfocused"/> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <Rectangle x:Name="fillColor" IsHitTestVisible="True" Opacity="0" RadiusY="1" RadiusX="1"/> 
         <Rectangle x:Name="fillColor2" IsHitTestVisible="True" Opacity="0" RadiusY="1" RadiusX="1"/> 
         <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}"/> 
         <Rectangle x:Name="FocusVisualElement" RadiusY="1" RadiusX="1" StrokeThickness="1" Visibility="Collapsed"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

回答

0

您需要修改或替換ControlTemplateListBoxItem並將其應用於您的Style。對於標誌本身,您將需要一些布爾屬性來表示值,以便可以在模板中使用它。我會推薦一個繼承的附屬財產,您可以在DragDropListBox上設置,然後訪問子女ListBoxItems

你絕對應該在XAML中做這個模板工作。在代碼中編寫ControlTemplate充其量只是乏味,並且阻止您使用現有模板作爲起點。如果你想在代碼中保留事件連接等事情,你可以從資源中獲取模板XAML,然後將它分配給代碼中的setter。

+0

我知道。只需看看編輯後的問題。 –

相關問題