Skip to content

Infragistics Community Forum / Cross Platform / Ultimate UI for Xamarin / VisualTreeHelper.GetChildrenCount returns 0

VisualTreeHelper.GetChildrenCount returns 0

New Discussion
charlie zhao
charlie zhao asked on Oct 11, 2013 8:01 AM

Hi, I have a xamDataChart, below is  the xaml code:

<ig:XamDataChart x:Name=”xmDataChart” SeriesMouseLeftButtonDown=”xmDataChart_SeriesMouseLeftButtonDown” Margin=”0,0,0,0″ Legend=”{Binding ElementName=xmLegend}”>
<ig:XamDataChart.Axes>
<ig:CategoryXAxis x:Name=”xmDateXAxis” Gap=”1″ Opacity=”0.3″ Interval=”1″ ItemsSource=”{Binding}” Label=”{}{MTime:yyyy-MM}”>
<ig:CategoryXAxis.LabelSettings>
<ig:AxisLabelSettings Location=”OutsideTop” Extent=”20″ />
</ig:CategoryXAxis.LabelSettings>
</ig:CategoryXAxis>
<ig:NumericYAxis x:Name=”xmWaterLevelYAxis” Opacity=”0.3″ IsInverted=”True” Label=” { }米”>
<ig:NumericYAxis.LabelSettings>
<ig:AxisLabelSettings Location=”OutsideLeft” Extent=”50″/>
</ig:NumericYAxis.LabelSettings>
</ig:NumericYAxis>
</ig:XamDataChart.Axes>
<ig:XamDataChart.Series>
<ig:LineSeries MarkerType=”Circle” Brush=”DarkRed” MarkerBrush=”DarkRed” x:Name=”lsManaNa” ValueMemberPath=”WaterLevel” ItemsSource=”{Binding}”
XAxis=”{Binding ElementName=xmDateXAxis}”
YAxis=”{Binding ElementName=xmWaterLevelYAxis}”>
<ig:LineSeries.ToolTip>
<StackPanel Orientation=”Horizontal”>
<TextBlock Text=”月份=”></TextBlock>
<TextBlock Text=”{Binding Item.MTime, StringFormat=yyyy年MM月}” Foreground=”{Binding ForegroundBrush}” />
<TextBlock Text=” , 水位= ” />
<TextBlock Text=”{Binding Item.WaterLevel,StringFormat=0.00}” Foreground=”{Binding ForegroundBrush}” />
<TextBlock Text=”米”/>
</StackPanel>
</ig:LineSeries.ToolTip>
<ig:LineSeries.LegendItemTemplate>
<DataTemplate>
<Border
BorderBrush=”Black”
BorderThickness=”1″
Margin=”2″
CornerRadius=”5″
Visibility=”{Binding Series.Visibility}”>
<StackPanel Orientation=”Horizontal”
Margin=”5″
Width=”150″>
<CheckBox x:Name=”WaterChangValue”
Click=”WaterChangValue_Click”
IsChecked=”True”
VerticalAlignment=”Center”
HorizontalAlignment=”Left” >
</CheckBox>
<ContentPresenter Content=”{Binding}”
ContentTemplate=”{Binding Series.LegendItemBadgeTemplate}” />
<ContentPresenter Content=”水位(米)”
Margin=”3″ />
</StackPanel>
</Border>
</DataTemplate>
</ig:LineSeries.LegendItemTemplate>
<ig:LineSeries.LegendItemBadgeTemplate>
<DataTemplate>
<Grid Width=”18″
Height=”16″
Margin=”0 0 2 0″>
<Grid.Effect>
<DropShadowEffect Color=”Black”
BlurRadius=”5″
ShadowDepth=”4″
Opacity=”0.5″></DropShadowEffect>
</Grid.Effect>
<Line VerticalAlignment=”Center”
HorizontalAlignment=”Center”
X1=”0″
Y1=”0″
X2=”16″
Y2=”0″
Stroke=”{Binding Series.ActualBrush}”
StrokeThickness=”{Binding Series.Thickness}”
StrokeEndLineCap=”{Binding Series.EndCap}”
StrokeStartLineCap=”{Binding Series.StartCap}”
Effect=”{Binding Series.Effect}” />
<ContentPresenter Width=”11″
Height=”11″
VerticalAlignment=”Center”
HorizontalAlignment=”Center”
Margin=”0 0 2 0″
ContentTemplate=”{Binding Series.ActualMarkerTemplate}”
Content=”{Binding}” />
</Grid>
</DataTemplate>
</ig:LineSeries.LegendItemBadgeTemplate>
</ig:LineSeries>
</ig:XamDataChart.Series>
</ig:XamDataChart>

I just want to access the ContentPresenter  control in LineSeries.LegendItemTemplate, change the control’s content, so I used the visualTreeHelper method, and below is my C# code:

a private method:

private void GetChildren(UIElement parent, Type targetType, ref List<UIElement> children)
{
int count = VisualTreeHelper.GetChildrenCount(parent);
if (count > 0)
{
for (int i = 0; i < count; i++)
{
UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
if (child.GetType() == targetType)
{
children.Add(child);
}
GetChildren(child, targetType, ref children);
}
}
}

Constructor method:

public UcGroundWater()
{
InitializeComponent();
List<UIElement> items = new List<UIElement>();
GetChildren(xmDataChart, typeof(ContentPresenter), ref items);
((ContentPresenter)items[1]).Content = “Water Level(m)”;
}

When I debug, I find VisualTreeHelper.GetChildrenCount(parent) always return 0,  so I can’t access the ContentPresenter  control.

Do I make myself clear?  Thanks in advance~~

Sign In to post a reply

Replies

  • 0
    charlie zhao
    charlie zhao answered on Oct 11, 2013 5:56 AM

    Anyone can help me ?   Thanks~~

    • 0
      Stefan
      Stefan answered on Oct 11, 2013 8:01 AM

      Hello Charlie,

       

      Thank you for your post. I have been looking into it and I can say that the behavior you have is expected, because the XamDataChart is not rendered by the time you execute the code for finding the child elements. If you put the code in Button’s Click handler you will see that the collection will get populated.

       

      Hope this helps you.

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
charlie zhao
Favorites
0
Replies
2
Created On
Oct 11, 2013
Last Post
12 years, 4 months ago

Suggested Discussions

Created by

Created on

Oct 11, 2013 8:01 AM

Last activity on

Feb 18, 2026 8:22 AM