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:
HTML
<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>


In both events ("_SELECCIONADO_Checked" and "_SELECCIONADO_Unchecked") I need to manipulate the Combox Item collection.

C#
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>


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:
C#
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?

Thanks
Posted
Updated 21-Aug-11 6:26am
v3

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
C#
CriteriosOpcoesComboChecks.ItemsSource
 
Share this answer
 
v2
Comments
Jorge J. Martins 20-Aug-11 20:32pm    
Simon

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:
C#
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;
}


And use it this way.
I have the Checked Event Handler on the CheckBox in the DataTemplate described on my initial question.

C#
        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
 
v2

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