我使用微软的WPF工具包中的Chart控件来编写自己的图表控件。我在博客上写了here。我的Chart控件将图表中的yaxes相互堆叠。正如你在文章中看到的,这一切都很好。现在我想创建一个视图模型来控制图表中的数据和坐标轴。到目前为止,我能够添加轴的图表,并显示在图表中。但是当我尝试添加lineseries时遇到了问题,因为它有一个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
}
字符串
视图模型都有自己的数据模板。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>
型
这段代码运行得很好。当我添加轴时,它们被绘制出来。但是lineseries控件的DependentAxis和InDependentAxis保持为null,因此不会绘制该系列。如何将嵌套的视图模型绑定到控件的属性?
2条答案
按热度按时间jtoj6r0c1#
你可能已经检查过了,但我发现当我调试绑定时,首先也是最容易的地方是从VS运行调试会话,因为调试输出会告诉哪些对象和属性绑定失败。我通常最终发现我需要显式地设置一个DataContext或其他类似于错字的东西。要查找的输出如下所示:
系统.Windows.数据错误:39:BindingExpression路径错误:
后面是你试图绑定到的属性名,通常最重要的是它实际试图绑定的类。如果这还没有帮助,这里有一篇关于调试绑定的很棒的文章:http://www.beacosta.com/blog/?p=52,其中讨论了Aelij提到的PresentationTraceSources.TraceLevel=High的使用,以及一些其他技术。希望这能走上正轨。
此致,
迈克
nzk0hqpo2#
应该可以的您可以检查以下几项: