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

时间:2021-04-09 00:03:26

I have added a content Control containing a telerik Chart which i have named bob. I have used xamlReader.parse to build the content control and then place it onto a canvas.

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

I now want to access the the chart called bob to set it's values.

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

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

I keep receiving null.

我一直收到空。

Could someone help me identify my chart and show me how to access it?

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

Code to add ContentControl to a canvas in my Xaml.

用于将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 个解决方案

#1


0  

If by 'named' you mean setting XAML property x:Name, you can reach it by this name. For example name="Control" -> Control.SomeProperty = ""

如果通过'named'表示设置XAML属性x:Name,则可以通过此名称访问它。例如name =“Control” - > Control.SomeProperty =“”

#1


0  

If by 'named' you mean setting XAML property x:Name, you can reach it by this name. For example name="Control" -> Control.SomeProperty = ""

如果通过'named'表示设置XAML属性x:Name,则可以通过此名称访问它。例如name =“Control” - > Control.SomeProperty =“”