来自itemtemplate的组合框项目集合操作 [英] Combobox Item collection manipulation from itemtemplate
问题描述
我有一个带有ItemTemplate的组合框:
I have a combobox with an ItemTemplate:
<combobox height="26" horizontalalignment="Left" margin="318,100,0,0" x:name="CriteriosOpcoesComboChecks" verticalalignment="Top" width="26" xmlns:x="#unknown">
<combobox.itemtemplate>
<datatemplate>
<stackpanel orientation="Horizontal">
<checkbox x:name="_SELECCIONADO" ischecked="{Binding SELECCIONADO, Mode=TwoWay}" checked="_SELECCIONADO_Checked" unchecked="_SELECCIONADO_Unchecked" />
<textblock x:name="_CAMPO_ID" text="{Binding CAMPO_ID}" visibility="Collapsed" />
<textblock x:name="_CAMPO_COMBO" text="{Binding CAMPO_COMBO}" />
</stackpanel>
</datatemplate>
</combobox.itemtemplate>
</combobox>
在两个事件中("_SELECCIONADO_Checked"和"_SELECCIONADO_Unchecked"),我都需要操作Combox项集合.
In both events ("_SELECCIONADO_Checked" and "_SELECCIONADO_Unchecked") I need to manipulate the Combox Item collection.
private void _SELECCIONADO_Checked(object sender, RoutedEventArgs e)
{
CheckBox chAll = sender as CheckBox;
List<app_get_group_by_campo_result> ItemList = new List<app_get_group_by_campo_result>();
//Do something with the Combobox Item list.
}</app_get_group_by_campo_result></app_get_group_by_campo_result>
>
我需要的帮助是如何以相对方式从CheckBox"chAll"开始,以相对方式将Combobox中的Item集合放入ItemList局部变量中.
我尝试使用:
The help I need is how to get the Item collection from the Combobox into the ItemList local variable in a relative manner starting on the CheckBox "chAll".
I tried to use:
DependencyObject t = sender as DependencyObject;
DependencyObject p = VisualTreeHelper.GetParent(t) as DependencyObject;
但是我被困在DataTemplate上,无法找到ComboBox的父级"方式.
任何人都可以提示如何工作吗?
谢谢
But I got stuck on the DataTemplate and couldn''t find a "parent" way up to the ComboBox.
Can anyone give a hint on how to get this working?
Thanks
推荐答案
我建议您阅读了解视觉效果WPF中的树和逻辑树 [ ^ ]了解逻辑树和可视树之间的区别.
但是,如果您在此处描述了不必要的情况,则可以执行
I would suggest you read Understanding the Visual Tree and Logical Tree in WPF[^] to understand the difference between the logical and visual tree.
But in the case you describe here that is not needed, you can just do
CriteriosOpcoesComboChecks.ItemsSource
这里是我的操作方式:
Here''s how I did it:
public object GetParentByType(string TypeOfParent, object ChildObject)
{
string LocalTypeOfParent = "System.Windows.Controls." + TypeOfParent;
DependencyObject SearchChildObject = ChildObject as DependencyObject;
DependencyObject ComparingObject = VisualTreeHelper.GetParent(SearchChildObject) as DependencyObject;
if (ComparingObject == null)
ComparingObject = ((FrameworkElement)SearchChildObject).Parent as DependencyObject;
if (ComparingObject.GetType().ToString() != LocalTypeOfParent)
return GetParentByType(TypeOfParent, ComparingObject as object);
else
return ComparingObject as object;
}
并以这种方式使用.
我在最初的问题中描述了DataTemplate中CheckBox上的Checked Event Handler.
And use it this way.
I have the Checked Event Handler on the CheckBox in the DataTemplate described on my initial question.
private void _SELECCIONADO_Checked(object sender, RoutedEventArgs e)
{ // I know that I'm looking for a ComboBox and I have to be sure it's there
ComboBox OQueProcuro = (ComboBox)GetParentByType("ComboBox", sender);
//Do something with the found object.
//ToDo: Aditional code has to inserted for error handling in case that there is no object of the specified type as a parent of the current object.
}
这篇关于来自itemtemplate的组合框项目集合操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!