Reputation: 2311
How to bind (Itemssource and selected item) of a combobox inside wpf datagrid? I am using MVVM pattern. I have tried
<DataGridTemplateColumn Header="Example 9">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding PartIds, RelativeSource={RelativeSource AncestorType=Window}}" SelectedItem="{Binding PartId, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
and
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding MyCars}" HorizontalAlignment="Left">
<DataGrid.Columns>
<DataGridTextColumn Header="Model" Binding="{Binding Model}"/>
<DataGridTextColumn Header="Registration" Binding="{Binding Registration}"/>
<DataGridTemplateColumn Header="Example 12">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding CarParts, RelativeSource={RelativeSource AncestorType=Window}}" DisplayMemberPath="PartName" SelectedValuePath="PartID" SelectedValue="{Binding PartId, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Selected" Binding="{Binding PartId}"/>
</DataGrid.Columns>
</DataGrid>
Properties used for data-binding
#region DataGrid List<String> Example
public ObservableCollection<MyCar> MyCars { get; set; }
public List<string> PartIds { get; set; }
#endregion
#region DataGrid List<Class> Example
public List<CarPart> CarParts { get; set; }
#endregion
Reference: http://code.msdn.microsoft.com/windowsdesktop/Best-ComboBox-Tutorial-5cc27f82
Upvotes: 4
Views: 17520
Reputation: 361
I've tried so many options but the easiest option i found is to generate load event of that combo box & Bind it with the list or data table.
e.g. In Xaml
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox x:Name="cmbPayee" Loaded="cmbPayee_Loaded" Text="{Binding PayeeName, NotifyOnSourceUpdated=True,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValue ="{Binding PayeeID, NotifyOnSourceUpdated=True,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath = "Payee1" SelectedValuePath="PayeeID"/>
</DataTemplate>
In .cs code
private void cmbPayee_Loaded(object sender, RoutedEventArgs e)
{
ComboBox cmb = (ComboBox)sender;
var res = from k in db.Payees
select k;
cmb.ItemsSource = res.ToList();
cmb.DisplayMemberPath = "Payee1";
cmb.SelectedValuePath = "PayeeID";
}
Upvotes: 6
Reputation: 841
Just a guess since I am not at my dev station, but try using ElementName to reference the window by name instead of relative source...
Something like:
ItemsSource="{Binding CarParts,ElementName=MyWindowName}"
and the add a Name="MyWindowName" to the window definition.
Upvotes: 1
Reputation: 3579
Try making your Lists observable collections. You need to make sure your properties are telling your UI when new objects are added to your collection and that is what an ObservableCollection
does for you.
Also make sure your CarPart
and MyCar
class implement INotifyPropertyChanged
.
Whether or not this is your problem depends on when exactly your properties are set.
Upvotes: 1