2011-10-03 5 views
1

ListBox에서 상속받은 클래스와 ListBoxItems에 대한 사용자 지정 ControlTemplate이 있습니다. 조건이 true이면 ListBoxItems 배경을 변경하려고합니다. 나는 이것을 위해 DataTrigger를 사용하려고 시도했다. ListBoxItems 컨텍스트 개체의 조건을 확인하고 싶지 않습니다. 상속 된 ListBox 클래스에서 확인하고 싶습니다.사용자 지정 템플릿이있는 listboxitem 트리거가 목록 상자에 바인딩

질문은 어떻게 런타임에 각 ListBoxItem에 대한 올바른 값을 결정해야 할 때 ControlTemplate을 트리거로 ListBox 속성에 바인딩 할 수 있습니까? 좋아, 먼저 몇 가지 제안

<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ListBoxItem"> 
        <Border Name="bd"> 
         <TextBlock Name="lbl" Text="{Binding Path=DataChar}" FontWeight="ExtraBold" FontSize="15" Margin="5"/> 
        </Border> 
        <ControlTemplate.Triggers> 
         <DataTrigger Binding="{Binding RelativeSource={ RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBox}}, Path=IsSymbolExists}" Value="True"> 
          <Setter TargetName="bd" Property="Background" Value="Yellow" /> 
         </DataTrigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 


public class CustomListBox : ListBox 
{ 
... 
    public bool IsSymbolExists 
    { 
      if(/*condition is true for the ListBoxItem*/) 
       return true; 
      return false; 
    } 
} 

답변

1

...

사용자 지정 목록 상자 컨트롤이 새로운 속성 (IsSymbolExists 등)와 진짜 행동을 가지고있다. 이 값 IsSymbolExists이 모든 해당 항목은 노란색 테두리로 개별적으로 강조 표시됩니다 목록 상자 마찬가지가된다 둘째 그래서 Attached Properties

로 선언하십시오. 이것은 잘 생각 UI의 동작처럼 보이지 않습니다. 미안해. 그게 너에게 좀 가혹한 것처럼 오면!

또한 바인딩 관점에서 DataChar 속성은 데이터 컨텍스트 기반 속성, 즉 일부 모델에서 오는 것처럼 보입니다. 그렇다면 ItemTemplateTextBlock이고 ControlTemplateListBoxItem이 아니라 ItemTemplate을 통해 ListBox에 바인딩해야합니다. 정확히 같은 이유로 DataTriggerControlTemplate에서 올바르게 작동하지 않습니다.

ItemTemplate에서 올바르게 작동합니다.

그래서

  1. 당신은 CustomListBox 제거 할 수 ... 코드를 이런 식으로 수정해야 요약합니다. MyListBoxBehavior.IsSymbolExists이라는 부울 첨부 속성을 만듭니다. 그것을 당신의 ListBox에 붙이십시오.

  2. ListBoxItemControlTemplate을 제거해야합니다.

이에서 목록 상자의 포획 helpm에서

(그대로이 코드는 늘 컴파일)이 도움이

<ListBox local:MyListBoxBehavior.IsSymbolExists="{Binding WhateverBoolean}" 
      ItemsSource="{Binding WhateverCollection}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Border> 
       <Border.Style> 
        <Style TargetType="{x:Type Border}"> 
         <Style.Triggers> 
         <DataTrigger 
           Binding="{Binding 
              RelativeSource={RelativeSource 
               Mode=FindAncestor, 
               AncestorType={x:Type ListBox}}, 
             Path=local:MyListBoxBehavior.IsSymbolExists}" 
           Value="True"> 
          <Setter Property="Background" Value="Yellow" /> 
         </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </Border.Style> 
       <TextBlock Name="lbl" 
          Text="{Binding Path=DataChar}" 
          FontWeight="ExtraBold" 
          FontSize="15" 
          Margin="5"/> 
       </Border> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

희망 :-).

관련 문제