2013-06-11 1 views
1

뷰 모델의 관찰 가능한 컬렉션에 바인딩 된 WPF 툴킷 DataGrid가 있습니다. 이 DataGrid에서는 해당 개체의 특정 필드에 대해 DataGridTemplateColumn을 정의했습니다. (Car.Name)WPF DataGrid CurrentItem이 null입니다.

중복 된 항목을 검색하고 다른 유사한 (유사한) 개체 목록에 이미있는 셀에 특정 스타일을 설정하려고합니다.

이 대화 상자가로드되면 선택 항목이 없습니다. 뷰 모델의 IsDuplicate는 행의 각 항목에 대해 호출되지만 뷰 모델에 현재있는 항목을 알 수 없습니다. 내가 CurrentItem을 사용하여 생각했지만, 그것은 항상 null로 보인다.

질문 : 현재 모델이 호출되는 모델을 View Model에서 어떻게 알 수 있습니까?

보기 XAML :

<toolkit:DataGrid ItemsSource="{Binding Cars}" 
         CurrentItem="{Binding CurrentCar}"> 

    ... 

    <toolkit:DataGridTemplateColumn.CellStyle> 
     <Style TargetType="{x:Type toolkit:DataGridCell}"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding 
         RelativeSource={RelativeSource FindAncestor, 
         AncestorType=toolkit:DataGrid}, 
         Path=DataContext.IsDuplicate}" Value="False"> 
      <Setter Property="BorderBrush" Value="Transparent" /> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding 
         RelativeSource={RelativeSource FindAncestor, 
         AncestorType=toolkit:DataGrid}, 
         Path=DataContext.IsDuplicate}" Value="True"> 
      <Setter Property="BorderBrush" Value="Red" /> 
      <Setter Property="BorderThickness" Value="3" /> 
      <Setter Property="ToolTip" Value="Duplicate" /> 
      </DataTrigger>     
     </Style.Triggers> 
     </Style> 
    </toolkit:DataGridTemplateColumn.CellStyle> 

ViewModel.cs :

public Car CurrentCar { get; set; } 

public bool IsDuplicate 
{ 
    get 
    { 
    // Logic to check current car against a list of cars 
    var x = CurrentCar; // null 
    } 
} 

| 이름 | ...

| 자동차 1 | ... < - 강조 표시

| 자동차 2 | ...

| 자동차 1 | ... < - 강조

답변

1

당신은 그것에 대해 잘못된 방법을 생각하고있다. 이것은 반복적 인 방법이 아니어야합니다. IsDuplicateCar의 속성이어야하며 컬렉션에 대한 링크가있어 각 Car 개체가 컬렉션에 일치하는 다른 항목이 있는지를 확인합니다. XAML에서 그런

public class Car 
{ 
    public Guid Id { get; set; } 
    public Collection<Car> Cars { get; set; } 
    public bool IsDuplicate 
    { 
     get 
     { 
      // Logic to check current car against a list of cars 
      return (Cars.Count(c => c.Id.Equals(this.Id))) > 1; 
     } 
    }  
} 

은 다음 XAML 바인딩 구문에 대한

<toolkit:DataGridTemplateColumn.CellStyle> 
    <Style TargetType="{x:Type toolkit:DataGridCell}"> 
    <Style.Triggers> 
     <DataTrigger Binding="IsDuplicate" Value="False"> 
     <Setter Property="BorderBrush" Value="Transparent" /> 
     </DataTrigger> 
     <DataTrigger Binding="IsDuplicate" Value="True"> 
     <Setter Property="BorderBrush" Value="Red" /> 
     <Setter Property="BorderThickness" Value="3" /> 
     <Setter Property="ToolTip" Value="Duplicate" /> 
     </DataTrigger>     
    </Style.Triggers> 
    </Style> 
</toolkit:DataGridTemplateColumn.CellStyle> 

너무 확실하지, 그건 그냥 내 머리 위로 떨어져 있습니다. 그러나 당신은 아이디어를 얻습니다.

1

시도 :

<toolkit:DataGrid ItemsSource="{Binding Cars}" 
        SelectedItem="{Binding CurrentCar}"> 
+0

대화 상자가로드되면 선택 항목이 없습니다. – mastofact

+0

@mastofact 다음 프로그램 적으로 항목을 선택하십시오. 'CurrentCar = Cars.FirstOrDefault()'. 무슨 뜻인지 모르겠다. –

+1

@mastofact, 나는 너무 푸근 해 보이려는 것이 아니지만, 귀하의 질문이 실제로 틀린 것 같아서, @HighCore가 여기에 요점을 놓치고있는 이유입니다. 나는 * 당신이 진술 한 목표에 따라 어떤 항목이 선택되었는지를 실제로 알고 싶지는 않을 것이라고 생각한다. "나는 중복을 탐지하고 (비슷한) 다른 객체 목록에 이미 존재하는 셀에 특정 스타일을 설정하려고한다. " 이것을 위해 필요한 것은 이것을 성취 할 수있는'Car'의 속성입니다. 특정 순서로 각 항목을 반복하지 않아야합니다. WPF가 반복을 처리합니다. – klugerama