views:

26

answers:

1

I have a usercontrol with a dependencyproperty that takes oen UIElement. So fare so good, the problem is I can not find the element's children.

I think the problem is my lack of knowledge, could anyone tell me what the problem is and a possible solution?

I have made a small test-program like this

Usercontrol codebehinde:

public UIElement TestSendUiElement
{
 get { return (StackPanel)GetValue(TestSendUiElementProperty); }
 set { SetValue(TestSendUiElementProperty, value); }
}

public static readonly DependencyProperty TestSendUiElementProperty =
 DependencyProperty.Register("TestSendUiElement", typeof(StackPanel), typeof(Test), new FrameworkPropertyMetadata(TestSendUiElementPropertyChanged));

private static void TestSendUiElementPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
 Console.WriteLine(VisualTreeHelper.GetChildrenCount((UIElement)e.NewValue));
}

xaml using the usercontrol:

<my:Test >
 <my:Test.TestSendUiElement>
  <StackPanel Orientation="Horizontal" Margin="0,2">
   <TextBox Height="23" Width="50" Margin="0,0,5,0" />
   <TextBox Height="23" Width="125" />
  </StackPanel>
 </my:Test.TestSendUiElement>
</my:Test>

Output is 0 children.

Shouldn't it be 2?

Pleas enlighten me.

A: 

I think it doesn't work because whatever you assign to the TestSendUiElement Dependency Property, it won't be part of the Visual Tree. So VisualTreeHelper.GetChildrenCount(...) will not work.

As a direct replacement, LogicalTreeHelper should do the trick.

And if you know the type of the object or can , then it's even better to use exposed properties like ItemsControl.Items, ContentControl.Content and etc., with the exception of classes ihneriting from Panel (they're LogicalChildren property is internal).

If you are lazy you could also do the following (untested code):

<my:Test.TestSendUiElement>
  <ItemsControl Margin="0,2">
    <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
        <StackPanel Orientation="Horizontal"/>
      </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <TextBox Height="23" Width="50" Margin="0,0,5,0" />
    <TextBox Height="23" Width="125" />
    <ItemsControl>
</my:Test.TestSendUiElement>

Then you change the type of the DP property to ItemsControl, and now you can access the children via this.TestSendUIElement.Items. An ItemsControl is probably not as lightweight as a panel, but using the LogicalTreeHelper probably wouldn't be optimal either. Depends on the scenario.

Alex Maker
LogicalTreeHelper seams to be the best way in my case. But when i tire this I only get this output: System.Windows.LogicalTreeHelper+EnumeratorWrapper.So i don't get any children i kan loop through. Should I get children?
Cinaird