Wpf 访问DataGridCell';是来自另一个DataGridCell的孩子吗?
我有一个包含组合框的DataGridCell 我希望,当我触发它的“SelectionChanged”事件时,不同列(最终-在运行时,cell)CellEditingTemplate资源的CollectionViewSource应该根据此行的选定值填充数据 也许是DataTrigger,ActionTrigger,EventTrigger,也许是代码,XAML我不在乎,我只需要一个解决方案 非常感谢 相关的:Wpf 访问DataGridCell';是来自另一个DataGridCell的孩子吗?,wpf,datagrid,binding,datagridcell,Wpf,Datagrid,Binding,Datagridcell,我有一个包含组合框的DataGridCell 我希望,当我触发它的“SelectionChanged”事件时,不同列(最终-在运行时,cell)CellEditingTemplate资源的CollectionViewSource应该根据此行的选定值填充数据 也许是DataTrigger,ActionTrigger,EventTrigger,也许是代码,XAML我不在乎,我只需要一个解决方案 非常感谢 相关的: 如果我没有弄错您的问题,您将根据在DataGrid的同一行中的另一个单元格中选择组合框
如果我没有弄错您的问题,您将根据在DataGrid的同一行中的另一个单元格中选择组合框来填充单元格中组合框的内容。 如果是: 第一种解决方案(最好是IMO) 制作一个表示行数据的ViewModel(围绕数据对象的简单包装器)。将目标组合框的ItemsSource属性绑定到从viewmodel提供的
IEnumerable
-属性。
将源组合框中的SelectedItem绑定到ViewModel的另一个属性。每次在ViewModel中更改此源属性时,都会更改ViewModel提供的列表的内容
用于指定(列表)属性a可观察集合
。源属性由您决定
这里是一个近似的例子。我调用VM类(用于ViewModel),但这不会改变当前解决方案。MVVM也可以部分使用
public class DataObjectVM : DependencyObject {
public static readonly DependencyProperty SelectedCategoryProperty =
DependencyProperty.Register("SelectedCategory", typeof(CategoryClass), typeof(DataObjectVM), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,delegate (DependencyObject d,DependencyPropertyChangedEventArgs e){
((DataObjectVM)d).SelectedCategoryChanged(e);
}));
ObservableCollection<ItemClass> _items=new ObservableCollection<ItemClass>();
void SelectedCategoryChanged(DependencyPropertyChangedEventArgs e) {
// Change here the contents of the _items collection.
// The destination ComboBox will update as you desire
// Do not change the _items reference. Only clear, add, remove or
// rearange the collection-items
}
// Bind the destination ComboxBox.ItemsSource to this property
public IEnumerable<ItemClass> DestinationItems {
get {
return _items;
}
}
// Bind to this property with the source ComboBox.SelectedItem
public CategoryClass SelectedCategory {
get { return (CategoryClass)GetValue(SelectedCategoryProperty); }
set { SetValue(SelectedCategoryProperty, value); }
}
}
这是我使用的标记:
<DataGrid.Columns>
<DataGridTemplateColumn Header="Source" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="PART_CboSource" SelectionChanged="CboSource_SelectionChanged" ItemsSource="!!YOUR ITEMS SOURCE!!" SelectedItem="{Binding Category}">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Destination">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="PART_CboDestination"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
访问CollectionViewSource
要访问CollectionViewSource,请将其放入相应DataTemplate的参考资料部分,而不是面板的参考资料部分,然后您将可以直接访问它们。IMO认为这个位置比网格的资源容器更合适
如果您不想这样做,请检查以下帖子的状态:
如果我正确理解您的问题,您将根据在DataGrid的同一行的另一个单元格中选择组合框来填充单元格中组合框的内容。 如果是: 第一种解决方案(最好是IMO) 制作一个表示行数据的ViewModel(围绕数据对象的简单包装器)。将目标组合框的ItemsSource属性绑定到从viewmodel提供的
IEnumerable
-属性。
将源组合框中的SelectedItem绑定到ViewModel的另一个属性。每次在ViewModel中更改此源属性时,都会更改ViewModel提供的列表的内容
用于指定(列表)属性a可观察集合
。源属性由您决定
这里是一个近似的例子。我调用VM类(用于ViewModel),但这不会改变当前解决方案。MVVM也可以部分使用
public class DataObjectVM : DependencyObject {
public static readonly DependencyProperty SelectedCategoryProperty =
DependencyProperty.Register("SelectedCategory", typeof(CategoryClass), typeof(DataObjectVM), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,delegate (DependencyObject d,DependencyPropertyChangedEventArgs e){
((DataObjectVM)d).SelectedCategoryChanged(e);
}));
ObservableCollection<ItemClass> _items=new ObservableCollection<ItemClass>();
void SelectedCategoryChanged(DependencyPropertyChangedEventArgs e) {
// Change here the contents of the _items collection.
// The destination ComboBox will update as you desire
// Do not change the _items reference. Only clear, add, remove or
// rearange the collection-items
}
// Bind the destination ComboxBox.ItemsSource to this property
public IEnumerable<ItemClass> DestinationItems {
get {
return _items;
}
}
// Bind to this property with the source ComboBox.SelectedItem
public CategoryClass SelectedCategory {
get { return (CategoryClass)GetValue(SelectedCategoryProperty); }
set { SetValue(SelectedCategoryProperty, value); }
}
}
这是我使用的标记:
<DataGrid.Columns>
<DataGridTemplateColumn Header="Source" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="PART_CboSource" SelectionChanged="CboSource_SelectionChanged" ItemsSource="!!YOUR ITEMS SOURCE!!" SelectedItem="{Binding Category}">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Destination">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="PART_CboDestination"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
访问CollectionViewSource
要访问CollectionViewSource,请将其放入相应DataTemplate的参考资料部分,而不是面板的参考资料部分,然后您将可以直接访问它们。IMO认为这个位置比网格的资源容器更合适
如果您不想这样做,请检查以下帖子的状态:
不幸的是,我没有在这个项目中使用MVVM,也没有MVVM的经验。可以用普通WPF来做吗?因为我的时间限制不允许我将整个项目重新创建为MVVM或学习MVVM。是的,你很明白,这是我的愿望。我的问题是关于第二个选择。我的问题只是查找部分,我不知道如何找到表兄组合框,我不想有一个硬编码的地址表,我仍然希望以某种方式保持它的动态…+1,听起来不错,看起来你正在接近我需要的,谢谢更新你的答案。仍然没有解决的是,我希望在(第二个)DataTemplate.Resources中包含CollectionViewSource,而不是控件。将帮助我搜索CollectionViewSource资源。另外,我正在谈论CellEditingTemplate。好的,需要的兄弟姐妹也在其中。编辑模板取自单元格模板(如果不可用)。其余的应该是容易的幸运的是,我没有在这个项目中使用MVVM,也没有MVVM的经验。可以用普通WPF来做吗?因为我的时间限制不允许我将整个项目重新创建为MVVM或学习MVVM。是的,你很明白,这是我的愿望。我的问题是关于第二个选择。我的问题只是查找部分,我不知道如何找到表兄组合框,我不想有一个硬编码的地址表,我仍然希望以某种方式保持它的动态…+1,听起来不错,看起来你正在接近我需要的,谢谢更新你的答案。仍然没有解决的是,我希望在(第二个)DataTemplate.Resources中包含CollectionViewSource,而不是控件。将帮助我搜索CollectionViewSource资源。另外,我正在谈论CellEditingTemplate。好的,需要的兄弟姐妹也在其中。编辑模板取自单元格模板(如果不可用)。剩下的应该很简单