繁体   English   中英

如何将嵌套的视图模型绑定到控件的属性

[英]How can I bind the nested viewmodels to properties of a control

我使用Microsoft的WPF工具包图表控件来编写自己的图表控件。 我在这里写博客。 我的图表控件将图表中的yaxs堆叠在一起。 你可以在文章中看到这一切都很有效。 现在我想创建一个控制图表中数据和轴的视图模型。 到目前为止,我可以将轴添加到图表中并在图表中显示它们。 但是当我尝试添加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
}

视图模型都有自己的datatemplate。 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保持为空,因此系列不会被绘制。 如何将嵌套的视图模型绑定到控件的属性?

它应该工作。 你可以检查几件事:

  • 系列装订是否有效? 如果是这样,试着弄清楚有什么区别。
  • 您确定XAxis和YAxis属性实际上具有值吗? 尝试在getter中放置一个断点。 如果已达到,则绑定有效。 您还可以在Binding上放置一个转换器(IValueConverter)(它只返回它接收的值)并在那里放置一个断点。
  • 在绑定上使用PresentationTraceSources.TraceLevel = High可以获得更详细的跟踪(将出现在VS Output窗口中)。
  • DependentAxis / IndependentAxis是否定义为FastLineSeries上的依赖项属性?

希望有所帮助,

Aelij。

您可能已经检查了这一点,但我发现当我调试绑定时,第一个也是最容易启动的地方是从VS运行调试会话,因为调试输出告诉哪些对象和属性无法绑定。 我通常最终发现我需要显式设置DataContext或其他类似拼写错误。 要查找的输出如下:

System.Windows.Data错误:39:BindingExpression路径错误:

接下来是您尝试绑定的属性名称,通常最重要的是它实际尝试绑定的类。 如果这没有帮助,那么这里有关于调试绑定的好文章: http//www.beacosta.com/blog/? p = 52,它讨论了Aelij提到的PresentationTraceSources.TraceLevel = High的使用,以及很少有其他技巧。 希望这能走上正轨。

问候,

麦克风

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM