我的应用程序中有一个数据网格。在这个网格中,我有两个datagridcombobox列。 一切都按预期工作,我唯一的努力是将第二个组合框的itemssource设置为第一个组合框的选定值,以获得对给定值的属性的访问权。
<UserControl.Resources>
<CollectionViewSource x:Key="itemcategoriesViewSource" d:DesignSource="{d:DesignInstance {x:Type local:itemcategories}, CreateList=True}"/>
<CollectionViewSource x:Key="maincategoriesViewSource" d:DesignSource="{d:DesignInstance {x:Type local:maincategories}, CreateList=True}"/>
</UserControl.Resources>
<Grid DataContext="{StaticResource itemcategoriesViewSource}">
<DataGrid x:Name="itemcategoriesDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" ItemsSource="{Binding Source={StaticResource itemcategoriesViewSource}}" EnableRowVirtualization="True" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn x:Name="nameColumn" Width="*" Header="name" Binding="{Binding name}"/>
<DataGridTextColumn x:Name="alternative_nameColumn" Width="Auto" Header="alternative name" Binding="{Binding alternative_name}"/>
<DataGridTextColumn x:Name="alternative_name2Column" Width="Auto" Header="alternative name 2" Binding="{Binding alternative_name2}"/>
<DataGridComboBoxColumn x:Name="maincategories_nameColumn" Width="Auto" Header="Main Category" ItemsSource="{Binding Source={StaticResource maincategoriesViewSource}}" DisplayMemberPath="name" SelectedItemBinding="{Binding subcategories.maincategories}"/>
<DataGridComboBoxColumn x:Name="subcategories_nameColumn" Width="Auto" Header="Sub Category" DisplayMemberPath="name" SelectedItemBinding="{Binding subcategories}">
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="ItemsSource/subcategories, ElementName="maincategories_nameColumn"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="ItemsSource/subcategories, ElementName="maincategories_nameColumn"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
代码背后:
public partial class MainWindow : Window
{
TestEntities _context = new TestEntities();
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
System.Windows.Data.CollectionViewSource maincategoriesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("maincategoriesViewSource")));
System.Windows.Data.CollectionViewSource itemcategoriesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("itemcategoriesViewSource")));
// Load data by setting the CollectionViewSource.Source property:
this._context.maincategories.ToList();
this._context.itemcategories.ToList();
maincategoriesViewSource.Source = this._context.maincategories.Local;
itemcategoriesViewSource.Source = this._context.itemcategories.Local;
}
}
数据库设置图片:http://i.stack.imgur.com/xIgO9.png - &gt;我不允许加入它。
这个想法很简单。我想显示itemcategories中所有值的列表(这就是我在网格中使用它作为datacontext的原因),在另一列中我想显示一个带有主要类别的下拉列表(选中的值是绑定的)到/ itemcategories / subcategories / maincategory_id),棘手的部分是根据选定的maincategory显示带有子类别的列。