Problems binding Silverlight DataGrid after updating or installing selectedIndex = -1

I have datagrid and combobox in form. The combobox is attached to the selected datagrid element.

I load everything in order, and if I select different lines, then combobox is updated accordingly.

If, however, I set datagrid.selectedIndex = -1 after loading (so the first row is not selected), the combobox binding no longer works. This is problem.

I also have another scenario where the exact thing happens. If I filter the datagrid, combobox binding also stops working.

I bind a datagrid to a CollectionViewSource as shown below, where _codes is an ObservableCollection

_ocvsCode = (CollectionViewSource) this.Resources ["cvsCode"]; _ocvsCode.Source = _codes; dataGrid1.ItemsSource = _ocvsCode.View;
Code>

I don't know why binding to combobox fails after some operation on a datagrid.

+3
source share
2 answers

The appropriate solution in this case is to bind the datagrid selecteditem to some variable and then bind other controls to this variable. It is generally a bad practice to associate UIElement properties directly with other UIElement properties. This will also debug the issue, which you seem to encounter forcing the selecteditem property in the combo box.

+2

, ComboBox SelectedItem DataGrid.

ComboBox , . , , , . DataGrid , SelectedItem null, SelectedItem . SelectedItem , ComboBox .

:

SelectedItem , DataContext. , null. DataGrid ComboBox.

public YourItem SelectedItem
{
    get { return _selectedItem; }
    set
    {
        if (value == _selectedItem || value == null)
            return;

        _selectedItem = value;
        RaisePropertyChanged("SelectedItem");
    }
}
0

Source: https://habr.com/ru/post/1778223/


All Articles