Frage

I have a wpf DataGrid.

On DataGrid the selected index is bind to a variable sitting in ViewModel.

Here's the code:

<DataGrid DataContext="{Binding MyViewModel}" SelectedIndex="{Binding SelectedXIndex,Mode=TwoWay}"  ItemsSource="{Binding xList, Mode=TwoWay}" AutoGenerateColumns="False" GridLinesVisibility="Horizontal">
    <DataGrid.Columns>
      ...
    </DataGrid.Columns>
</DataGrid>

In ViewModel I have a list of items from class X (Observable Collection), and I have the selected index variable:

    private int _selectedXIndex;
    public int SelectedXIndex
    {
        get { return _selectedXIndex; }
        set
        {
            _selectedXIndex = value;
            NotifyPropertyChanged("SelectedXIndex");
        }
    }

When I click on an item in the DataGrid selected index changes, and when I initialize it in constractor of the ViewModel is changed in DataGrid, but when I initialize it on a certain function is changed only in the ViewModel and not in the DataGrid:

  public void MoveDown()
    {
        SelectedXIndex++;         
    }

I can not understand why this is happening!

EDIT:

I added to the function the following line of code:

xList[SelectedXIndex].Name = "Changest Name For Test";

and it works.

He changes the name on the line that Selected Item of the model on it, but he did not show at DataGird that the selected item is the one.

War es hilfreich?

Lösung

Unless I am mistaken, by doing SelectedXIndex++ you aren't resetting that SelectedXIndex again, so the NotifyPropertyChanged won't be called.

Either do: SelectedXIndex = _selectedXIndex++;

OR:

Continue with what you have and just call a notify right after that increment in MoveDown.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top