我使用Microsoft的图表控件的WPF工具包来编写我自己的图表控件。 我对此博文here。我的图表控件将图表中的yax堆叠在一起。正如你可以在文章中看到的,这一切都很好。现在我想创建一个控制图表中数据和坐标轴的视图模型。到目前为止,我可以将轴添加到图表并在图表中显示它们。但是当我尝试添加线路系列时,我遇到了一个问题,因为它有一个DependentAxis和一个InDependentAxis属性。我不知道如何为它分配正确的xAxis和yAxis控件。
下面你看到LineSeriesViewModel的一部分。它有一个嵌套的XAxisViewModel和YAxisViewModel属性。如何将嵌套的视图模型绑定到控件的属性
public class LineSeriesViewModel : ViewModelBase, IChartComponent
{
XAxisViewModel _xAxis;
public XAxisViewModel XAxis
{
get { return _xAxis; }
set
{
_xAxis = value;
RaisePropertyChanged(() => XAxis);
}
}
//The YAxis Property look the same
}
该viewmodels都有自己的数据模板。 的XAML代码如下所示:
<UserControl.Resources>
<DataTemplate x:Key="xAxisTemplate" DataType="{x:Type l:YAxisViewModel}">
<chart:LinearAxis x:Name="yAxis" Orientation="Y" Location="Left" Minimum="0" Maximum="10" IsHitTestVisible="False" Width="50" />
</DataTemplate>
<DataTemplate x:Key="yAxisTemplate" DataType="{x:Type l:XAxisViewModel}">
<chart:LinearAxis x:Name="xAxis" Orientation="X" Location="Bottom" Minimum="0" Maximum="100" IsHitTestVisible="False" Height="50" />
</DataTemplate>
<DataTemplate DataType="{x:Type l:LineSeriesViewModel}">
<!--Binding doesn't work on the Dependent and IndependentAxis! -->
<!--YAxis XAxis and Series are properties of the LineSeriesViewModel -->
<l:FastLineSeries DependentAxis="{Binding Path=YAxis}"
IndependentAxis="{Binding Path=XAxis}"
ItemsSource="{Binding Path=Series}"/>
</DataTemplate>
<Style TargetType="ItemsControl">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<!--My stacked chart control -->
<l:StackedPanel x:Name="stackedPanel" Width="Auto" Height="Auto" Background="LightBlue">
</l:StackedPanel>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ClipToBounds="True">
<!-- View is an ObservableCollection of all axes and series-->
<ItemsControl x:Name="chartItems" ItemsSource="{Binding Path=View}" Focusable="False">
</ItemsControl>
</Grid>
此代码工作得很好。当我添加轴时,他们会绘制。但是系列控件的DependentAxis和InDependentAxis保留为null,所以系列不会被绘制。我如何将嵌套的viewmodels绑定到控件的属性?