WPF 基于不同的数据库表将 ComboBox 添加到 DataGrid 或列表视图 [英] WPF Adding a ComboBox to a DataGrid or List View Based on different DB Tables
问题描述
我想将 ComboBox
添加到 DataGrid
或 ListView
但组合框的底层 ViewSource
将是由 DataGrid
的每一行的 DataRowView
中的数据过滤.
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
.
示例:
公司列表和公司的一些信息显示在 DataGrid
/ListView
中.列出的公司可能有多个电话号码.我希望电话号码位于与公司信息一起显示的 ComboBox
中.公司信息和电话号码在不同的表格中,所有数据的绑定方式只是一种方式.
A list of companies and some information about the company is displayed in a 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.
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?
谢谢!!
推荐答案
我会这样做
查看:
<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}"/>
视图模型:
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; }
}
型号:
public class Company
{
public string CompanyName { get; set; }
public List<string> CompanyPhoneNumbers { get; set; }
}
我在这个例子中使用了 Prism 框架.i
和 ie
是命名空间的快捷方式:
I used Prism framework in this example. The i
and ie
are shortcuts to namespaces:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:ie="http://schemas.microsoft.com/expression/2010/interactivity"
这里发生的事情是 viewModel 保存了一个选定公司电话号码的列表.每当公司发生变化时(在此示例中选择了网格中的不同行),所选公司的电话号码也会相应更改.
What happens here is that the viewModel holds a list of the selected company phone numbers. 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.
这里有一些关于 MVVM 的好链接:
Here are some good links on MVVM:
http://www.codeproject.com/Articles/165368/WPF-MVVM-快速入门教程
http://www.codeproject.com/Articles/126249/MVVM-Pattern-in-WPF-A-Simple-Tutorial-for-Absolute
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
祝你好运
这篇关于WPF 基于不同的数据库表将 ComboBox 添加到 DataGrid 或列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!