如何在 wpf 数据网格中绑定组合框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20692403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to bind a combobox inside wpf datagrid?
提问by user2330678
How to bind (Itemssource and selected item) of a combobox inside wpf datagrid? I am using MVVM pattern. I have tried
如何在 wpf 数据网格中绑定(Itemssource 和所选项目)的组合框?我正在使用 MVVM 模式。我试过了
<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
参考:http: //code.msdn.microsoft.com/windowsdesktop/Best-ComboBox-Tutorial-5cc27f82
回答by Pranjal Jain
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
例如在 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
在 .cs 代码中
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";
}
回答by ansible
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 ObservableCollectiondoes for you.
尝试使您的 Lists 成为可观察的集合。您需要确保您的属性在将新对象添加到您的集合时通知您的 UI,而这正是ObservableCollection您所做的。
Also make sure your CarPartand MyCarclass implement INotifyPropertyChanged.
还要确保您的CarPart和MyCar班级实现INotifyPropertyChanged.
Whether or not this is your problem depends on when exactly your properties are set.
这是否是您的问题取决于您的属性是何时设置的。
回答by NebulaSleuth
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...
只是猜测,因为我不在我的开发站,但尝试使用 ElementName 按名称而不是相对源引用窗口...
Something like:
就像是:
ItemsSource="{Binding CarParts,ElementName=MyWindowName}"
and the add a Name="MyWindowName" to the window definition.
并将 Name="MyWindowName" 添加到窗口定义中。
回答by Aashutosh
Just Use this os .cs side
只需使用此 os .cs 端
DataContext = CarParts;
DataContext = CarParts;

