根据另一个datagrid组合框柱过滤WPF datagrid组合框 [英] Filter WPF datagrid comboboxcolumn based on another datagrid comboboxcolumn

查看:85
本文介绍了根据另一个datagrid组合框柱过滤WPF datagrid组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows Presentation Foundation中有一个带有两个组合框的数据网格。第一个组合框是省,第二个是区。我在SQL中有适当关系的省和地区的表。我想只填充数据网格中所选省份的区域。我在网上看了很多文章,但无法弄清楚这一点。我希望有人会帮忙。



这是数据网格的XAML。



 <   DataGrid     x:名称  =  dgBranches   背景  = 白色    BorderBrush   = #5ac7ff    Horizo​​ntalAlignment   =    保证金  =  34,321,0,0    VerticalAlignment   = 顶部   高度  =   120   宽度 < span class =code-keyword> =  600    Grid.Row   =  1    Grid.Column   =  1    Grid.ColumnSpan   =   2   网格LinesVisibility   = 所有    AutoGenerateColumns   =  False    IsSynchronizedWithCurrentItem   =  True >  
< DataGrid.Columns >
< DataGridCheckBoxColumn Binding = {Binding MainBranch} ClipboardCon tentBinding = {x:Null} 标题 = 主要分支 宽度 = 100 / >
< span class =code-keyword>< DataGridComboBoxColumn SelectedValueBinding = {Binding ProvinceID} x:名称 = cbxBranch DisplayMemberPath = SelectedValuePath = ProvinceID 标题 = / >
< DataGridComboBoxColumn SelectedItemBinding = {绑定区域ID} x:名称 = cbxDistrict DisplayMemberPath = SelectedValuePath < span class =code-keyword> = DistrictID 标题 = ItemsSource = {绑定路径=区}} / >

< DataGridTextColumn B. inding = {Binding Village} 标题 = Village / >
< DataGridTextColumn 绑定 = {Binding Location} 标题 = 位置 / >
< DataGridTextColumn Binding = {Binding NameOfMarket} 标题 = 市场名称 / >
< span class =code-keyword><
DataGridTextColumn Binding = {Binding ShopNo} 标题 = 商店数字 / >
< / DataGrid.Columns >
< / DataGrid >







和填充组合框的代码是





 私有  Sub  PopulateProvinces()
Dim qry As String = 选择*来自省份

Dim a As < span class =code-keyword>新 SqlDataAdapter(qry,ConnectionRefDB.DBSql)
Dim d 作为 DataTable

a.Fill(d)

.cbxIssuedFrom.ItemsSource = d.DefaultView
.cbxBranch.ItemsSource = d.DefaultView
结束 Sub

私人 Sub PopulateDistricts()

Dim qry 作为 字符串 = 选择*来自地区

Dim a As SqlDataAdapter(qry,ConnectionRefDB.DBSql)
Dim d As DataTable

a.Fill(d)

.cbxDistrict.ItemsSource = d.DefaultView
结束 Sub

解决方案

< blockquote>嗨Ahmad,



首先让我们将你的需求翻译成一个模式:



基本上你需要按所选省份更改地区=>主细节模式。



省是大师,区是基于大师的细节。



所以现在我们可以看到我们需要Master-Detail模式。



但最好的方法是什么?好吧,我可以看到你正在使用DataGrid并逐个构建你的列...所以让我们考虑一下......为什么我们不应该构建一个简单的数据模板?我们为什么要使用DataGrid?



在我的文章中:



如何:在WPF分层DataTemplate中使用Master-Detail模式



您可以看到我不需要将DataGrid用于类似网格的UI结构。使用带有数据模板的ItemsControl要容易得多:



 <   ItemsControl     ItemsSource   =  {Binding Items} >  
< ItemsControl.ItemTemplate >
< DataTemplate >
< StackPanel 方向 = 水平 MinWidth = 320 >
< TextBlock 宽度 = 130 文字 = {Binding Name} / >
< ComboBox 宽度 = 130 ItemsSource = {Binding Employees} SelectedIndex = {Binding EmployeesSelectedIndex} / >
< ComboBox 宽度 = 130 ItemsSource = {Binding Subordinates} / >
< / StackPanel >
< / DataTemplate >
< / ItemsControl.ItemTemplate >
< / ItemsControl >

I have a datagrid in Windows Presentation Foundation with two comboboxes. The first combobox is Provinces and the second is Districts. I have tables in SQL for provinces and districts with proper relations. I want to populate only the districts for the selected provinces in the datagrid. I have looked at many articles on the web, but couldn't figure this out. I hope someone will help.

Here is the XAML for the datagrid.

<DataGrid x:Name="dgBranches" Background="White" BorderBrush="#5ac7ff" HorizontalAlignment="Left" Margin="34,321,0,0" VerticalAlignment="Top" Height="120" Width="600" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" GridLinesVisibility="All" AutoGenerateColumns="False" IsSynchronizedWithCurrentItem="True">
            <DataGrid.Columns>
                <DataGridCheckBoxColumn Binding="{Binding MainBranch}" ClipboardContentBinding="{x:Null}" Header="Main Branch" Width="100"/>
                <DataGridComboBoxColumn SelectedValueBinding="{Binding ProvinceID}" x:Name="cbxBranch" DisplayMemberPath="Province" SelectedValuePath="ProvinceID" Header="Province" />
                <DataGridComboBoxColumn SelectedItemBinding="{Binding DistrictID}" x:Name="cbxDistrict" DisplayMemberPath="District" SelectedValuePath="DistrictID" Header="District" ItemsSource="{Binding Path=Districts}" />

                <DataGridTextColumn Binding="{Binding Village}" Header="Village"/>
                <DataGridTextColumn Binding="{Binding Location}" Header="Location"/>
                <DataGridTextColumn Binding="{Binding NameOfMarket}" Header="Market Name"/>
                <DataGridTextColumn Binding="{Binding ShopNo}" Header="Shop Number"/>
            </DataGrid.Columns>
</DataGrid>




And the code to populate the comboboxes is


Private Sub PopulateProvinces()
    Dim qry As String = "Select * From Provinces"

    Dim a As New SqlDataAdapter(qry, ConnectionRefDB.DBSql)
    Dim d As New DataTable

    a.Fill(d)

    Me.cbxIssuedFrom.ItemsSource = d.DefaultView
    Me.cbxBranch.ItemsSource = d.DefaultView
End Sub

Private Sub PopulateDistricts()

    Dim qry As String = "Select * From Districts"

    Dim a As New SqlDataAdapter(qry, ConnectionRefDB.DBSql)
    Dim d As New DataTable

    a.Fill(d)

    Me.cbxDistrict.ItemsSource = d.DefaultView
End Sub

解决方案

Hi Ahmad,

First lets translate your need to a pattern:

Basically you need to change districts by the selected province => Master-Detail Pattern.

Province is the Master and the districts are the Details based on the Master.

So now we can see the we need Master-Detail Pattern.

But what is the best way to do that? Well I can see that you are using DataGrid and build your columns one by one... So lets think about it... Why shouldn't we build a simple Data-Template? And why should we use a DataGrid?

In my article:

How to: Use the Master-Detail Pattern with WPF Hierarchical DataTemplate

You can see that I don't need to use DataGrid for a grid-like UI structure. It's much more easy to use ItemsControl With Data-Template:

<ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" MinWidth="320">
                <TextBlock Width="130" Text="{Binding Name}"/>
                <ComboBox Width="130" ItemsSource="{Binding Employees}" SelectedIndex="{Binding EmployeesSelectedIndex}" />
                <ComboBox Width="130" ItemsSource="{Binding Subordinates}" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>


这篇关于根据另一个datagrid组合框柱过滤WPF datagrid组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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