Reputation: 147
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?
Upvotes: 3
Views: 3034
Reputation: 4774
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.
Upvotes: 6