0

I have datagrid with two columns: text and combobox. And combobox should have binding to observable collection.

This is pseudocode for datagrid items source:

    public class ModeObjectState
    {
        public int ID { get; set; }
        public int ObjectTypeID { get; set; }
        public string State { get; set; }
    }

    public class ModeObject
    {
         public string Name { get; set; }
         public int objID { get; set; }
         public int Type { get; set; }
         public int StateID { get; set; }
         public bool Format { get; set; }
    }

    public class _dataContext
    {
        public ObservableCollection<ModeObjectState> ListObjectState { get; set; }
        public ModeObject ModeObj { get;  set; }
    }

    ObservableCollection<_dataContext> SourceObjList
    objTable.ItemsSource = SourceObjList;

This is xaml code for datagrid:

    <DataGrid x:Name="objTable" AutoGenerateColumns="False" >
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="ColumnName" Binding="{Binding Path=ModeObj.Name}" IsReadOnly="True" />
            <DataGridComboBoxColumn x:Name="ColumnState" ItemsSource="{Binding ListObjectState}" DisplayMemberPath="State" SelectedValuePath="ID" SelectedValueBinding="{Binding Path=ModeObj.StateID}" /> 
        </DataGrid.Columns>
    </DataGrid>

But datagrid doesn't show any items in comboboxcolumn. Please, help me with binding the datagridcombobox to observable collection "ListObjectState" in "_dataContext" class.

Thanks!

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
drmckay-
  • 215
  • 1
  • 2
  • 11

2 Answers2

0

Implement with INotifyPropertyChanged for the _dataContext

public class _dataContext : INotifyPropertyChanged
    {
        private ObservableCollection<ModeObjectState> _listObjectState;

        public ObservableCollection<ModeObjectState> ListObjectState
        {
            get { return _listObjectState; }
            set
            {
                _listObjectState = value; 
                OnPropertyChagned("ListObjectState");
            }
        }

        public ModeObject ModeObj { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChagned(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Since the 1st set may done after the Binding, so it wont affect the UI..

Sankarann
  • 2,625
  • 4
  • 22
  • 59
0

Its difficult to figure out without looking at the whole code. You have binding issue, and it would be easier to find where the issue is using XAML debugging tools like Snoop or WPF Inspector. You just need to attach your running application to see the Datacontext.

You can easily find if the datacontext is valid or not. WPF Inspector has a better User interface, but its prone to crash. Press Ctrl+Shift and hover mouse over your control to see it getting reflected in Snoop/WPF Inspected.

Also see your Output window for whats the binding error you are getting.

Carbine
  • 7,849
  • 4
  • 30
  • 54