使用实体框架在wpf datagrid中级联组合框

时间:2014-02-15 13:02:08

标签: c# wpf

我正在使用实体框架模型在数据模板列(而不是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;
}

提前多多感谢

1 个答案:

答案 0 :(得分:0)

这是一个开始 如果州是在一个县,那么把它们放在国家

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