0
我添加了一個內容控件,其中包含一個名爲bob的telerik圖表。 我已經使用xamlReader.parse來構建內容控件,然後將其放置在畫布上。如何訪問通過名稱添加到C#的控件
我現在想訪問名爲bob的圖表來設置它的值。
var myChart = (RadCartesianChart)this.FindName("bob");
我一直收到null。
有人能幫我確定我的圖表並告訴我如何訪問它嗎?
將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);
您能否顯示您的XAML? –
您是否在創建時命名?當被添加到表單中時,它不會保留變量名稱作爲其名稱,而必須明確設置它。也是笛卡爾圖表* rad * –
@ S.Akbari我已經展示了將字符串解析爲xaml的C#代碼 – Pomster