我是MVVM的新手。目前我所有的代码都是以链接到XAML的.cs文件编写的。我想切换到MVVM,但遇到困难。我将尝试解释为什么:切换到MVVM问题
我有很多不同的图表控件和输入数据,以.cs文件中指定的方式直接访问Chart对象并使用它的属性programaticaly为我的图表添加点。
例子:
foreach (var group in qcv.Groups)
{
AreaSeries areaSeries = new AreaSeries();
areaSeries.CombineMode = Telerik.Charting.ChartSeriesCombineMode.Stack;
areaSeries.ValueBinding = new PropertyNameDataPointBinding("Rev");
areaSeries.CategoryBinding = new PropertyNameDataPointBinding("Date");
areaSeries.ItemsSource = group as IEnumerable;
RadChart1.Series.Add(areaSeries);
}
但只要我切换到MVVM RadChart1
对象变得不可访问的视图模型文件。我怎样才能让它在ViewModel类中可见,或者你可以建议更好的方法,我怎样才能得到该对象,并为我的图表提供输入而无需更改我的代码?
我的XAML文件:
<UserControl x:Class="FrontEnd.RevenueChart"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FrontEnd"
mc:Ignorable="d" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" HorizontalAlignment="Stretch" >
<UserControl.DataContext>
<local:RevenueChartViewModel/>
</UserControl.DataContext>
<Grid>
<telerik:RadCartesianChart HorizontalAlignment="Stretch" x:Name="RadChart1" Palette="Metro" Zoom="10,1">
<telerik:RadCartesianChart.HorizontalAxis>
<telerik:CategoricalAxis/>
</telerik:RadCartesianChart.HorizontalAxis>
<telerik:RadCartesianChart.VerticalAxis>
<telerik:LinearAxis/>
</telerik:RadCartesianChart.VerticalAxis>
<telerik:RadCartesianChart.Behaviors>
<telerik:ChartPanAndZoomBehavior ZoomMode="Both">
</telerik:ChartPanAndZoomBehavior>
</telerik:RadCartesianChart.Behaviors>
</telerik:RadCartesianChart>
</Grid>
</UserControl>
嗨!我现在无法整合一个例子,但请看看这里:http://www.telerik.com/help/silverlight/radchart-how-to-mvvm-support.html,这会有帮助吗?让我知道这是否适合你! MVVM是def。要走的路 – 2013-02-24 02:02:03
虹膜,谢谢你的回答。我现在正在研究这个例子,我会试着弄清楚那里发生了什么。谢谢! – 2013-02-24 02:54:28