뷰 모델의 관찰 가능한 컬렉션에 바인딩 된 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 | ... < - 강조
대화 상자가로드되면 선택 항목이 없습니다. – mastofact
@mastofact 다음 프로그램 적으로 항목을 선택하십시오. 'CurrentCar = Cars.FirstOrDefault()'. 무슨 뜻인지 모르겠다. –
@mastofact, 나는 너무 푸근 해 보이려는 것이 아니지만, 귀하의 질문이 실제로 틀린 것 같아서, @HighCore가 여기에 요점을 놓치고있는 이유입니다. 나는 * 당신이 진술 한 목표에 따라 어떤 항목이 선택되었는지를 실제로 알고 싶지는 않을 것이라고 생각한다. "나는 중복을 탐지하고 (비슷한) 다른 객체 목록에 이미 존재하는 셀에 특정 스타일을 설정하려고한다. " 이것을 위해 필요한 것은 이것을 성취 할 수있는'Car'의 속성입니다. 특정 순서로 각 항목을 반복하지 않아야합니다. WPF가 반복을 처리합니다. – klugerama