2014-09-05 3 views
0

나는 다음과 같은 목록 상자가 - 전체 데이터 컨텍스트가 BatchRefC#/XAML 하이라이트 행

<ListBox x:Name="Details" 
    ItemsSource="{Binding BatchRef.ScheduleGroups}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
        <localData:ScheduleGroupControl></localData:ScheduleGroupControl> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
</ListBox> 

BatchRef는 CurrentID 필드가 클래스로 이동을하고, ScheduleGroup 클래스는 ID 필드가 . ScheduleGroup ID 필드가 BatchRef 필드의 CurrentID와 일치하는지 확인하는 방법을 찾고 있습니다. 목록 상자 항목 중 하나는 항상 상위 항목과 일치하므로 올바른 행을 강조 표시해야합니다.

+0

데이터 트리거가있는 스타일 – Paparazzi

답변

1

이렇게하려면 DataTrigger을 사용할 수 있습니다.

public class IntEqualConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return values.OfType<int>().Distinct().Count() == 1; 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 
} 

이것은 원유 예입니다 : 당신의 ItemTemplate이 같은

뭔가가 : 두 값이 동일한 경우

<ListBox ItemsSource="{Binding Current.Groups}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding Path=CurrentId, 
              StringFormat={}CurrentID: {0}}"/> 
       <TextBlock Text="{Binding Path=DataContext.Current.CurrentId, 
              StringFormat={}ParentID: {0}, 
              RelativeSource={RelativeSource AncestorType=Grid}}"/> 

       <StackPanel.Style> 
        <Style TargetType="StackPanel"> 
         <Setter Property="Background" Value="Red"/> 
         <Style.Triggers> 
          <DataTrigger Value="True"> 
           <DataTrigger.Binding> 
            <MultiBinding Converter="{StaticResource IntEqualConverter}"> 
             <Binding Path="CurrentId"/> 
             <Binding Path="DataContext.Current.CurrentId" 
               RelativeSource="{RelativeSource AncestorType=Grid}"/> 
            </MultiBinding> 
           </DataTrigger.Binding> 
           <DataTrigger.Setters> 
            <Setter Property="Background" Value="Green"/> 
           </DataTrigger.Setters>     
          </DataTrigger>           
         </Style.Triggers> 
        </Style> 
       </StackPanel.Style> 

      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

내가 확인하는 IMultiValueConverter을 사용했습니다. 그러나 그것은 올바른 방향으로 당신을 가리켜 야합니다. 주의 할

중요한 사항은 다음과 같습니다 그렇지 않으면 DataContext를가 ListBoxItem의 그것 일 것이다

  • 당신은 바인딩에 RelativeSource를 사용할 필요가 없습니다보기의 DataContext.
  • 변환기를 리소스로 추가해야합니다.

Example

그것은 이해하는 데 도움합니다. 내 ViewModelCurrent 속성은 BatchRef 개체입니다. BatchRef 클래스에는 Groups이라는 유형이 있으며 ObservableCollection<ScheduleGroup> 유형의 속성이 있습니다.

+0

감사합니다. - 계속 진행되는 것처럼 보입니다 - 귀하의 예는 매우 철저합니다! –

+0

문제 없습니다. 어떤 문제가 있다면 알려주고 내가 도울 수 있는지 알게 될 것입니다. –

관련 문제