繁体   English   中英

使用实体框架在WPF DataGrid中级联组合框

[英]cascading comboboxes in wpf datagrid using entity framework

我正在使用实体框架模型在数据模板列(而不是datagridcomboboxcolumn)中使用组合框。 在datagrid的第一列中。 组合框应显示国家/地区,第二栏中应显示状态。 当国家/地区连续更改时,第二列组合中的州必须按照该行中的国家/地区进行更新。 我有三个表,即country(country_id int,country_name),state(country_id,state_id,state_name)和district(country_id,state_id,district_id,district_name)。 我有四个.cs文件

1:testEntities.cs

public partial class testEntities : DbContext
{
    public testEntities()
        : base("name=testEntities")
    {
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }
    public DbSet<country> countries { get; set; }
    public DbSet<district> districts { get; set; }
    public DbSet<state> states { get; set; }
}

* 2:-country.cs *

public partial class country
{
    public byte country_id { get; set; }
    public string country_name { get; set; }
}

3:-state.cs

public partial class state
{
    public Nullable<byte> country_id { get; set; }
    public byte state_id { get; set; }
    public string state_name { get; set; }
}

4:-district.cs

public partial class district
{
    public Nullable<byte> country_id { get; set; }
    public Nullable<byte> state_id { get; set; }
    public byte district_id { get; set; }
    public string district_name { get; set; }
}

和一个XAML文件

<Window.Resources>
    <CollectionViewSource x:Key="countryViewSource" d:DesignSource="{d:DesignInstance {x:Type local:country},CreateList=True}"/>
    <CollectionViewSource x:Key="statetViewSource" d:DesignSource="{d:DesignInstance {x:Type local:state}, CreateList=True}"/>
    <CollectionViewSource x:Key="districtViewSource" d:DesignSource="{d:DesignInstance {x:Type local:district}, CreateList=True}"/>
</Window.Resources>
<Grid >
    <DataGrid AutoGenerateColumns="False"
              ItemsSource="{Binding}"
              DataContext="{StaticResource districtViewSource}"
              HorizontalAlignment="Left"
              Margin="47,45,0,0"
              VerticalAlignment="Top"
              Height="179" Width="421">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Country" SortMemberPath="country_id" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding country_name}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding}" 
                                  DataContext="{StaticResource countryViewSource}" 
                                  SelectedValue ="{Binding country_id, Mode=TwoWay}" 
                                  DisplayMemberPath="country_name" SelectedValuePath="country_id" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>

            <DataGridTemplateColumn Header="State" SortMemberPath="state_id">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding state_name}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding}"  
                                  DataContext="{StaticResource statetViewSource}" 
                                  DisplayMemberPath="state_name"
                                  SelectedValuePath="state_id"
                                  SelectedValue="{Binding state_id, Mode=TwoWay}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>

            <DataGridTextColumn x:Name="district_idColumn" Binding="{Binding district_id}" Header="district id" Width="SizeToHeader"/>

            <DataGridTextColumn x:Name="district_nameColumn" Binding="{Binding district_name}" Header="district name" Width="SizeToHeader"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

xaml背后的代码非常感谢

using System.Windows.Data;
private void Window_Loaded(object sender, RoutedEventArgs e)
{           
    testEntities te= new testEntities ();
    CollectionViewSource countryViewSource = ((CollectionViewSource)(this.FindResource("countryViewSource")));
    CollectionViewSource stateViewSource = ((CollectionViewSource)(this.FindResource("stateViewSource")));
    CollectionViewSource districtViewSource = ((CollectionViewSource)(this.FindResource("districtViewSource")));

    te.countries .Load();
    te.states .Load ();
    te.districts .Load ();

    countryViewSource.Source = te.countries.Local;
    stateViewSource.Source = te.states.Local;
    districtViewSource.Source = te.countries.Local;
}

提前非常感谢

这是一个开始
如果州在一个县中,则将其放入该国

public partial class country
{
    public byte country_id { get; set; }
    public string country_name { get; set; }
    public DbSet<state> states { get; set; }
}

然后在cbStates绑定中使用如下语法

ItemsSource="{Binding ElementName=cbCountries, Path=SelectedItem.States}"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM