如何按名称访问从C#添加的控件

时间:2016-04-07 13:22:45

标签: c# wpf xamlreader

我添加了一个内容控件,其中包含一个名为bob的telerik图表。 我使用xamlReader.parse构建内容控件,然后将其放在画布上。

我现在想要访问名为bob的图表来设置它的值。

var myChart = (RadCartesianChart)this.FindName("bob");

我一直收到空。

有人可以帮我识别我的图表并告诉我如何访问它吗?

将ContentControl添加到我的Xaml中的画布的代码。

StringBuilder LineCharString = new StringBuilder("<ContentControl ");
LineCharString.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");
LineCharString.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
LineCharString.Append("xmlns:s='clr-namespace:AccSys.Helpers.Dashboard;assembly=AccSys' ");
LineCharString.Append("xmlns:telerik='http://schemas.telerik.com/2008/xaml/presentation' ");
LineCharString.Append("Width='100' Height='100' MinWidth='50' MinHeight='50' Canvas.Top='160' Canvas.Left='30'>");
LineCharString.Append("<Grid IsHitTestVisible='False'>");
LineCharString.Append("<Grid.RenderTransform>");
LineCharString.Append("<TranslateTransform/>");
LineCharString.Append("</Grid.RenderTransform>");
LineCharString.Append("<telerik:RadCartesianChart x:Name='" + ChartName + "'>");
LineCharString.Append("<telerik:RadCartesianChart.Grid>");
LineCharString.Append("<telerik:CartesianChartGrid MajorLinesVisibility='XY' MajorXLineDashArray='10,5' MajorXLinesRenderMode='All' />");
LineCharString.Append("</telerik:RadCartesianChart.Grid>");
LineCharString.Append("<telerik:RadCartesianChart.VerticalAxis>");
LineCharString.Append("<telerik:LinearAxis/>");
LineCharString.Append("</telerik:RadCartesianChart.VerticalAxis>");
LineCharString.Append("<telerik:RadCartesianChart.HorizontalAxis>");
LineCharString.Append("<telerik:CategoricalAxis/>");
LineCharString.Append("</telerik:RadCartesianChart.HorizontalAxis>");
LineCharString.Append("<telerik:BarSeries x:Name='xBarSeries' CategoryBinding='XValue' ValueBinding='YValue'/>");
LineCharString.Append("</telerik:RadCartesianChart>");
LineCharString.Append("</Grid>");
LineCharString.Append("</ContentControl>");

ContentControl LineChartCC = new ContentControl();
LineChartCC = (ContentControl)XamlReader.Parse(LineCharString.ToString());

var canvass = DashboardCanvas;
canvass.Children.Add(LineChartCC);

1 个答案:

答案 0 :(得分:0)

如果被命名为&#39;你的意思是设置XAML属性x:名称,你可以通过这个名称来访问它。例如name =&#34; Control&#34; - &GT; Control.SomeProperty =&#34;&#34;