Home > Net >  MVVM Datagrid ComboBox DataSource not Updating on Selection
MVVM Datagrid ComboBox DataSource not Updating on Selection

Time:12-07

I have a datagrid in an MVVM project which has two ComboBox columns. I have a DataTable as it's ItemsSource, as well as an ObservableObject Collection as the source of the ComboBox items. There is an instance of the ObservableObject for Selected Item.

While I am able to drop down and select the items in the boxes, my problem is when an item is selected it is not updating the DataTable records with that selection. Example if in a record I want to set a column to "Computer" in the ComboBox, looking at the value in that record later it shows as an empty cell instead of Computer.

What am I doing wrong in terms of binding the SelectedItem and then updating the underlying DataTable with that new selection and new record?

    public class AddGroupObjectTypeModel : ObservableObject
    {
        #region Properties
        private int iD;
        public int ID
        {
            get
            {
                return iD;
            }
            set
            {
                if (iD != value)
                {
                    iD = value;
                    OnPropertyChanged();
                }
            }
        }

        private string objType;
        public string ObjType
        {
            get
            {
                return objType;
            }
            set
            {
                if (objType != value)
                {
                    objType = value;
                    OnPropertyChanged();
                }
            }
        }
        #endregion
    }
        private readonly ObservableCollection<AddGroupObjectTypeModel> objectTypeTerms = new()
        {
            new AddGroupObjectTypeModel { ID = 1, ObjType = "User" },
            new AddGroupObjectTypeModel { ID = 2, ObjType = "Group" },
            new AddGroupObjectTypeModel { ID = 3, ObjType = "Computer" }
        };

        public IEnumerable<AddGroupObjectTypeModel> ObjectTypeTerms
        {
            get { return objectTypeTerms; }
        }

        private AddGroupObjectTypeModel selectedObjectTypeTerm = new();
        public AddGroupObjectTypeModel SelectedObjectTypeTerm
        {
            get { return selectedObjectTypeTerm; }
            set { selectedObjectTypeTerm = value; OnPropertyChanged(); }
        }

        private DataTable objectsDisplayTable;
        public DataTable ObjectsDisplayTable
        {
            get { return objectsDisplayTable; }
            set { objectsDisplayTable = value; OnPropertyChanged(); }
        }

            ObjectsDisplayTable = new("Temporary Table");
            ObjectsDisplayTable.Columns.Add("ObjectName", typeof(String));
            ObjectsDisplayTable.Columns.Add("OType", typeof(String));
            ObjectsDisplayTable.Columns.Add("ODomain", typeof(String));
    <DataGrid x:Name="ObjectsTable"
                                          AutoGenerateColumns="False"
                                          Grid.Row="1"
                                          CanUserResizeColumns="True"
                                          Margin="2,0,0,0" 
                                          Style="{StaticResource ThemedDataGrid}"
                                          BorderBrush="{x:Null}"
                                          ItemsSource="{Binding ObjectsDisplayTable,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                          HorizontalAlignment="Stretch" 
                                          VerticalAlignment="Stretch"
                                          CanUserAddRows="True"
                                          SelectionMode="Single"
                                          SelectionUnit="Cell" 
                                          >

<DataGridTemplateColumn Header="Type"
                                                        x:Name="OType"
                                                        MinWidth="200"
                                                        >
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <ComboBox ItemsSource="{Binding Path=DataContext.ObjectTypeTerms, RelativeSource={RelativeSource AncestorType=DataGrid}}"
                                                      DisplayMemberPath="ObjType"
                                                      SelectedItem="{Binding Path=DataContext.SelectedObjectTypeTerm.ObjType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType=DataGrid}}"
                                                      SelectedValue="{Binding Path=DataContext.SelectedObjectTypeTerm.ID, RelativeSource={RelativeSource AncestorType=DataGrid}}"
                                                      SelectedValuePath="ID"
                                                      SelectedIndex="0"
                                                      IsSynchronizedWithCurrentItem="false"
                                                      Style="{StaticResource ComboBoxTheme}"
                                                       />
                                        </DataTemplate>

CodePudding user response:

You should bind the SelectedValue property of the ComboBox to the column of the DataTable that you want to update:

<ComboBox ItemsSource="{Binding Path=DataContext.ObjectTypeTerms, RelativeSource={RelativeSource AncestorType=DataGrid}}"
          DisplayMemberPath="ObjType"
          SelectedValuePath="ObjType"
          SelectedValue="{Binding OType, UpdateSourceTrigger=PropertyChanged}"
          SelectedIndex="0"
          Style="{StaticResource ComboBoxTheme}" />

It makes no sense to bind to both SelectedValue and SelectedItem.

  • Related