3

Please help me to figure out how to work with ComboBoxColumn in WPF's DataGrid. I'm trying to create a list of devices, where each device have dynamic list of states in field "log".

<DataGrid AutoGenerateColumns="False" Margin="12,6,12,12" Name="dataGrid1" Grid.Row="1"  SelectionUnit="FullRow">
    <DataGrid.Columns>
            ...
         <DataGridComboBoxColumn Header="Log" 
                                 ItemsSource="{Binding log, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:Device}}}"/>
    </DataGrid.Columns>
</DataGrid>

public partial class MainWindow : Window
{
    public ObservableCollection<Device> devices;
    ...
}

public MainWindow() 
{
    ...
    dataGrid1.ItemSource = devices;
}

public class Device : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    public Device() {log = new ObservableCollection<string>();}
    ...
    private ObservableCollection<string> _log;
    public ObservableCollection<string> log { get { return _log; } 
                                              set { _log = value; OnPropertyChanged("log"); } }
}

Can you share any suggestions: How can i show in each combobox in datagrid list "log" of each object?

icebat
  • 4,696
  • 4
  • 22
  • 36
ies.kras
  • 147
  • 1
  • 7
  • first check that is your datagridcolumn is retrieving log or not by applying breaking point on get method of log(collection). if it is going on that break point then check is you log(collection) is empty or not.just check it. – loop Jul 10 '13 at 09:34
  • log(collection) is definetly not empty in runtime. But DataGridComboBoxColumn not retrieves log. – ies.kras Jul 10 '13 at 09:43
  • so thr is problem with your binding means in relative source part..you are not able to give a proper path where your log exists. so just put your whole windows.cs code here i will check it out and hopefully make it correct for you – loop Jul 10 '13 at 09:49
  • I agree, that error can be only in binding: `` May be you know, how to bind datagridcombobox to datagrids' datacontext? – ies.kras Jul 10 '13 at 09:54
  • ok fine..just post your windows.cs code here ..i will cehck it out – loop Jul 10 '13 at 10:02
  • put code of MainWindow class here .. – loop Jul 10 '13 at 10:26

1 Answers1

6

MSDN: DataGridComboboxColumns says:

To populate the drop-down list, first set the ItemsSource property for the ComboBox by using one of the following options:

  • A static resource. For more information, see StaticResource Markup Extension.
  • An x:Static code entity. For more information, see x:Static Markup Extension.
  • An inline collection of ComboBoxItem types.

So basically to just bind to data object`s collection property it`s better to use DataGridTemplateColumn:

<DataGridTemplateColumn Header="Log">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
             <ComboBox ItemsSource="{Binding log}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

This type of column gives you some more posibilities for templating too.

icebat
  • 4,696
  • 4
  • 22
  • 36