Reputation: 2937
I have a WPF DataGrid in my view with a column that I want to be able to edit using a combobox. Do to so, I create a property on my viewmodel like this:
public List<EnumeradorWCFModel> TiposCarga { get; set; }
The property is filled correctly using a WCF service. Now this is my DataGrid definition:
<DataGrid ItemsSource="{Binding Path=TarifarioSel.TarifariosDet}"
IsReadOnly="False" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridComboBoxColumn Width="200" Header="Tipo Carga" ItemsSource="{Binding Path=TiposCarga}"
SelectedValueBinding="{Binding Path=ID_TipoCarga}" DisplayMemberPath="Descripcion" SelectedValuePath="ID"/>
</DataGrid.Columns>
I Also try this, with no luck:
<DataGrid ItemsSource="{Binding Path=TarifarioSel.TarifariosDet}"
IsReadOnly="False" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridComboBoxColumn Width="200" Header="Tipo Carga" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}}, Path=DataContext.TiposCarga}"
SelectedValueBinding="{Binding Path=ID_TipoCarga}" DisplayMemberPath="Descripcion" SelectedValuePath="ID"/>
</DataGrid.Columns>
The problem is how can i bind the ItemsSource Property of the combobox column to a property inside my viewmodel.
Any help please! Thanks!
Upvotes: 0
Views: 1920
Reputation: 2937
Ok. Find it myself, but don't know why it has to be made like this. I post the answer in case someone needs it.
<DataGrid ItemsSource="{Binding Path=TarifarioSel.TarifariosDet}"
IsReadOnly="False" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridComboBoxColumn Width="200" Header="Tipo Carga"
SelectedValueBinding="{Binding Path=ID_TipoCarga}" DisplayMemberPath="Descripcion" SelectedValuePath="ID">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.TiposCarga}"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.TiposCarga}"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
Upvotes: 2