[英]cascading comboboxes in wpf datagrid using entity framework
I am using combo boxes in data template column (not datagridcomboboxcolumn) using entity framework model. 我正在使用实体框架模型在数据模板列(而不是datagridcomboboxcolumn)中使用组合框。 In first column of datagrid. 在datagrid的第一列中。 combobox should display countries and in second coloumn the combobox should display states. 组合框应显示国家/地区,第二栏中应显示状态。 when country is changed in a row the states in second column combo must be updated as per country in that row. 当国家/地区连续更改时,第二列组合中的州必须按照该行中的国家/地区进行更新。 I have three tables viz country(country_id int,country_name) , state(country_id, state_id , state_name), and district(country_id, state_id ,district_id, district_name). 我有三个表,即country(country_id int,country_name),state(country_id,state_id,state_name)和district(country_id,state_id,district_id,district_name)。 I have four .cs files as 我有四个.cs文件
1:testEntities.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 * * 2:-country.cs *
public partial class country
{
public byte country_id { get; set; }
public string country_name { get; set; }
}
3:-state.cs 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 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; }
}
and one xaml file 和一个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>
the code behind xaml is lot of thanks in advance 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;
}
thanks a lot in advance 提前非常感谢
Here is a start 这是一个开始
If states are in a county then put them in the country 如果州在一个县中,则将其放入该国
public partial class country
{
public byte country_id { get; set; }
public string country_name { get; set; }
public DbSet<state> states { get; set; }
}
Then in cbStates binding use syntax like this 然后在cbStates绑定中使用如下语法
ItemsSource="{Binding ElementName=cbCountries, Path=SelectedItem.States}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.