[英]WPF Adding a ComboBox to a DataGrid or List View Based on different DB Tables
I would like to add a ComboBox
to a DataGrid
or ListView
but the Combobox's underlying ViewSource
would be filtered by data from each row's DataRowView
of the DataGrid
. 我想将
ComboBox
添加到DataGrid
或ListView
但是Combobox的基础ViewSource
将由DataGrid
每行DataRowView
中的数据过滤。
Example: 例:
A list of companies and some information about the company is displayed in a DataGrid
/ ListView
. 公司列表和有关公司的一些信息显示在
DataGrid
/ ListView
。 The companies listed may have several phone numbers. 列出的公司可能有几个电话号码。 I want the phone numbers to be in the
ComboBox
that is displayed with the companies information. 我希望电话号码在与公司信息一起显示的
ComboBox
。 The company information and phone numbers are in different tables and the binding Mode would only be one way for all data. 公司信息和电话号码在不同的表中,并且绑定模式仅是所有数据的一种方式。
Or is there a better way to display the data? 还是有更好的方式显示数据?
Thanks!! 谢谢!!
I would do something like this 我会做这样的事情
View: 视图:
<DataGrid x:Name="myGrid" ItemsSource="{Binding Companies}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding CompanyName}"/>
</DataGrid.Columns>
<ie:Interaction.Triggers>
<ie:EventTrigger EventName="SelectionChanged">
<ie:InvokeCommandAction Command="{Binding SelectedItemChangedCommand}" CommandParameter="{Binding ElementName=myGrid, Path=SelectedItem}"/>
</ie:EventTrigger>
</ie:Interaction.Triggers>
</DataGrid>
<ComboBox ItemsSource="{Binding SelectedCompanyPhoneNumbers}"/>
ViewModel: ViewModel:
public class MainWindowViewModel
{
public MainWindowViewModel()
{
SelectedItemChangedCommand = new DelegateCommand<object>((selectedItem) =>
{
var selected = selectedItem as Company;
SelectedCompanyPhoneNumbers = selected.CompanyPhoneNumbers;
});
}
public view LoadCompanies()
{
// Load the companies information from different tables ...
}
public List<Company> Companies { get; set; }
public DelegateCommand<object> SelectedItemChangedCommand { get; set; }
public List<string> SelectedCompanyPhoneNumbers { get; set; }
}
Model: 模型:
public class Company
{
public string CompanyName { get; set; }
public List<string> CompanyPhoneNumbers { get; set; }
}
I used Prism framework in this example. 在此示例中,我使用了Prism框架。 The
i
and ie
are shortcuts to namespaces: i
和ie
是名称空间的快捷方式:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:ie="http://schemas.microsoft.com/expression/2010/interactivity"
What happens here is that the viewModel holds a list of the selected company phone numbers. 此处发生的是viewModel包含所选公司电话号码的列表。 Whenever the company gets changed (a different row in the grid get's selected in this example) the selected company's phone numbers is changed accordingly.
每当公司更改时(在此示例中选择了网格中的另一行),所选公司的电话号码也会相应更改。
Here are some good links on MVVM: 这是MVVM上的一些良好链接:
http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial
http://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute http://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx http://msdn.microsoft.com/zh-CN/magazine/dd419663.aspx
Good luck 祝好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.