WPF Adding a ComboBox to a DataGrid or List View Based on different DB Tables

故事扮演 提交于 2019-12-20 04:38:22

问题


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.

Example:

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?

Thanks!!


回答1:


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:

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. 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"

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.

Here are some good links on MVVM:

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://msdn.microsoft.com/en-us/magazine/dd419663.aspx

Good luck



来源:https://stackoverflow.com/questions/18874258/wpf-adding-a-combobox-to-a-datagrid-or-list-view-based-on-different-db-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!