Hodaya Shalom
Hodaya Shalom

Reputation: 4417

Selected item is not synchronizing with selected items - a WPF DataGrid

How can it be that SelectedItem is null and SelectedItems have an item selected?

Here's the screenshot from selection changed event:

enter image description here

My DataGrid:

 <DataGrid SelectionChanged="CustomCmdDg_SelectionChanged" SelectedItem="{Binding CurrentX,Mode=TwoWay}" DataContext="{Binding MyViewModel}" x:Name="CustomCmdDg"  ItemsSource="{Binding xList}" AutoGenerateColumns="False" GridLinesVisibility="Horizontal">

... In my ViewModel:

xList= a list of class x (observable collection)

  private x currentX;

    public x CurrentX
    {
        get { return currentX; }
        set
        {
            currentX = null;
            NotifyPropertyChanged("CurrentX");
        }
    }

Purposely wanted that selected item will be null

Upvotes: 2

Views: 1180

Answers (1)

JMan
JMan

Reputation: 2629

If you set your currentitem to null you should first remove it from the collection, then it will be gone from the selected items:

Public ObservableCollection<x> xList

public x CurrentX
{
    get { return currentX; }
    set
    {
        xList.Remove(currentX)
        currentX = null;
        NotifyPropertyChanged("CurrentX");
    }
}

Your observable list will update itself

If you need to be able to manipulate the SelectedItems collection you will have to provide a binding as well and peform the code required

Upvotes: 2

Related Questions