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?

6
  • 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. Commented Jul 10, 2013 at 9:34
  • log(collection) is definetly not empty in runtime. But DataGridComboBoxColumn not retrieves log. Commented Jul 10, 2013 at 9: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 Commented Jul 10, 2013 at 9:49
  • I agree, that error can be only in binding: <DataGridComboBoxColumn Header="Log" ItemsSource="{Binding log, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:Device}}}"/> May be you know, how to bind datagridcombobox to datagrids' datacontext? Commented Jul 10, 2013 at 9:54
  • ok fine..just post your windows.cs code here ..i will cehck it out Commented Jul 10, 2013 at 10:02

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.