Reputation: 69
I want to bind 'Units' property of ResultModel Class to DataGridComboBoxColumn. Other Bindings work fine but it cannot resolve 'Units' property. It recommends me 'Results' List instead of 'Units'. I want to use different Lists in each row.
You can see my View, ViewModel and Model Classes below.
<DataGrid x:Name="DataGrid" AutoGenerateColumns="False" Grid.Row="1" ItemsSource="{Binding Results}">
<DataGrid.Columns>
<DataGridTextColumn Header="#" Width="20" Binding="{Binding Path=Count}" />
<DataGridTextColumn Header="Name" Width="*" Binding="{Binding Path=ComponentName}" />
<DataGridTextColumn Header="Variable" Width="*" Binding="{Binding Path=Property}" />
<DataGridTextColumn Header="Value" Width="*" Binding="{Binding Path=PropertyValue}" />
<DataGridComboBoxColumn Header="Unit" Width="*" ItemsSource="{Binding Units}"/>
</DataGrid.Columns>
</DataGrid>
private List<ResultModel> _results;
public List<ResultModel> Results
{
get { return _results; }
set
{
_results = value;
RaisePropertyChanged("Results");
}
}
private int _count;
public int Count
{
get { return _count; }
set { _count = value; RaisePropertyChanged("Count"); }
}
private string _componentName;
public string ComponentName
{
get { return _componentName; }
set { _componentName = value; RaisePropertyChanged("ComponentName"); }
}
private string _property;
public string Property
{
get { return _property; }
set { _property = value; RaisePropertyChanged("Property"); }
}
private double _propertyValue;
public double PropertyValue
{
get { return _propertyValue; }
set { _propertyValue = value; RaisePropertyChanged("PropertyValue"); }
}
private List<object> _units;
public List<object> Units
{
get { return _units; }
set { _units = value; RaisePropertyChanged("Units"); }
}
private Type _componentType;
public Type ComponentType
{
get { return _componentType; }
set { _componentType = value; RaisePropertyChanged("ComponentType"); }
}
Upvotes: 2
Views: 1323
Reputation: 17001
I had to use a DataGridTemplateColumn
that contained a ComboBox
to get it to work properly. This assumes the object
in Units
has a Name
property to display, you should adjust DisplayMemberPath
accordingly. You will pribably also want to bind SelectedItem
to something, so you know what item they picked.
<DataGridTemplateColumn Header="Unit">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="local:ResultModel">
<ComboBox
DisplayMemberPath="Name"
ItemsSource="{Binding Path=Units}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Upvotes: 4
Reputation: 169420
Try this:
<DataGridComboBoxColumn Header="Unit" Width="*">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Units}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Units}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
You may also want to set the DisplayMemberPath
property of the DataGridComboBoxColumn
to the name of a property of the units.
Upvotes: 1