基于不同的数据库表WPF添加一个ComboBox DataGrid或列表视图 [英] WPF Adding a ComboBox to a DataGrid or List View Based on different DB Tables

查看:197
本文介绍了基于不同的数据库表WPF添加一个ComboBox DataGrid或列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个组合框添加到的DataGrid 的ListView ,但组合框的基本 ViewSource 将通过数据过滤每行的在<$ C $的 DataRowView的 C>的DataGrid 。

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 。上市公司可能有多个电话号码。我要电话号码是在显示与本公司的信息组合框
公司信息和电话号码,在不同的表和结合模式将仅是所有数据的一种方法

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; }
}

我在此实施例中使用棱镜框架。在 I 是快捷方式命名空间:

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"

这里发生的是,视图模型持有选定的公司的电话号码的列表。每当公司得到(网格GET在这个例子中选择了不同的行)所选择的公司的电话号码改变时相应地改变。

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.$c$ cproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial

http://www.$c$cproject.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆