2014-04-09 1 views
0

사용자 지정 목록 상자를 만들고 부울을 기반으로 몇 가지 항목에 대해 마우스를 가리 키거나 마우스를 선택하지 못하게하려고합니다. 스타일 클래스를 사용하고 사용자 정의 목록 상자의 코드에서 스타일을 설정합니다. 내 코드는 다음과 같습니다 :CustomListbox에서 선택한 항목에 대해 마우스 가져 오기 및 선택 사용 안 함

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; 
    }   
} 

지금 당장 나는 drop 이벤트에 대한 이벤트 설정기를 설정하고 올바르게 작동합니다. 플래그를 기반으로 항목에 마우스를 가져 가거나 마우스 선택 효과를 사용하지 않도록 스타일을 설정하려면 어떻게해야합니까? 내가 찾은 대부분의 코드는 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

수정하거나 ListBoxItemControlTemplate를 교체해야 변환 어떻게 귀하의 Style에 그것을 적용하십시오. 플래그 자체에 대해 템플릿에 사용할 수 있도록 값을 표현하기 위해 bool 속성이 필요할 것입니다. DragDropListBox에서 설정 한 다음 자식 ListBoxItems에 액세스 할 수있는 상속 된 속성을 사용하는 것이 좋습니다.

XAML에서이 템플릿 작업을 반드시 수행해야합니다. 코드에 ControlTemplate을 쓰는 것은 지겨운 일이므로 기존 템플릿을 시작점으로 사용할 수 없습니다. 코드에서 이벤트 연결을 유지하려는 경우 리소스에서 XAML 템플릿을 가져 와서 코드의 setter에 할당 할 수 있습니다.

+0

나는 그것을 안다. 편집 된 질문을 살펴보십시오. –

관련 문제