Reputation: 550
I have a WPF toolkit DataGrid that is bound to an observable collection of objects in the view model. In this DataGrid, I have defined a DataGridTemplateColumn for a certain field of that object. (Car.Name)
I'm trying to detect duplicates and set a certain style on the cell that already exists in another list of (similar) objects.
When this dialog gets loaded there is no selection. The IsDuplicate in the view model does get called for each item of the row, but I am unable to tell which item it's currently on in the view model. I thought of using CurrentItem, but it seems to always be null.
Question: How do I know in the View Model which current item is being called?
View 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
}
}
| Name | ...
| Car 1 | ... <-- Highlight
| Car 2 | ...
| Car 1 | ... <-- Highlight
Upvotes: 3
Views: 2065
Reputation: 3352
You're thinking about it the wrong way. This shouldn't be an iterative method. IsDuplicate
needs to be a property of Car
, with a link to the collection so that each Car
object determines if there are other items in the collection that match it.
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;
}
}
}
Then in 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>
Not so sure about the XAML binding syntax, that's just off the top of my head. But you get the idea.
Upvotes: 1
Reputation: 44038
Try:
<toolkit:DataGrid ItemsSource="{Binding Cars}"
SelectedItem="{Binding CurrentCar}">
Upvotes: 1