Click here to Skip to main content
15,851,701 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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">
            <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}" />

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.

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;

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?

Updated 21-Aug-11 6:26am

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
Share this answer
Jorge J. Martins 20-Aug-11 20:32pm    

Thanks for Your answer.
The thing is that my next step will be creating a UserControl to be used in a DataGrid and all ComboBoxes in that DataGrid would be named "CriteriosOpcoesComboChecks" and that is what I need the relative path for.
I looked at the link You suggested but Silverlight doesn't have the LogicalTreeHelper Class.
Simon Bang Terkildsen 20-Aug-11 20:41pm    
Ok didn't know you used Silverlight.
You can then use the VisualTreeHelper in combination with FrameworkElement.Parent. So when VisualTreeHelper.GetParent returns null use FrameworkElement.Parent
Jorge J. Martins 20-Aug-11 21:18pm    
That did the trick! Thanks.
RaisKazi 21-Aug-11 0:55am    
My 5.
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);
        return ComparingObject as object;

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.

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900