VisualTreeHelper.GetChildrenCount returns 0
New DiscussionHi, 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~~