Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid CurrentItem is null

Tags:

c#

mvvm

wpf

xaml

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

like image 516
mastofact Avatar asked Nov 19 '25 21:11

mastofact


1 Answers

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.

like image 92
klugerama Avatar answered Nov 21 '25 12:11

klugerama