C# 如何将嵌套的viewmodels绑定到控件的属性

C# 如何将嵌套的viewmodels绑定到控件的属性,c#,wpf,data-binding,datatemplate,wpftoolkit,C#,Wpf,Data Binding,Datatemplate,Wpftoolkit,我使用微软WPF工具包的图表控件来编写自己的图表控件。 我在博客上写了这件事。我的图表控件将图表中的雅克斯堆叠在一起。正如你在文章中看到的,这一切都很好。现在我想创建一个viewmodel,用于控制图表中的数据和轴。到目前为止,我能够将轴添加到图表中并在图表中显示它们。但我在尝试添加线系列时遇到了一个问题,因为它有一个DependentAxis和一个indpendentaxis属性。我不知道如何为其分配适当的xAxis和yAxis控件。 下面是LineSeriesViewModel的一部分。它具

我使用微软WPF工具包的图表控件来编写自己的图表控件。 我在博客上写了这件事。我的图表控件将图表中的雅克斯堆叠在一起。正如你在文章中看到的,这一切都很好。现在我想创建一个viewmodel,用于控制图表中的数据和轴。到目前为止,我能够将轴添加到图表中并在图表中显示它们。但我在尝试添加线系列时遇到了一个问题,因为它有一个DependentAxis和一个indpendentaxis属性。我不知道如何为其分配适当的xAxis和yAxis控件。
下面是LineSeriesViewModel的一部分。它具有嵌套的XaxiViewModel和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>


这段代码运行得很好。当我添加轴时,它们会被绘制出来。但是lineseries控件的DependentTaxis和IndendentAxis保持为空,因此不会绘制该系列。如何将嵌套的viewmodels绑定到控件的属性?

应该可以。您可以检查以下几点:

  • 这个系列的装订行吗?如果是这样,试着找出区别
  • 您确定XAxis和YAxis属性确实有值吗?尝试在getter中放置断点。如果达到了,绑定就会起作用。您还可以在绑定上放置一个转换器(IValueConverter)(它只返回它接收到的值)并在那里放置一个断点
  • 在绑定上使用PresentationOnTraceSources.TraceLevel=High可获得更详细的跟踪(将显示在VS输出窗口中)
  • DependentTaxis/IndendentAxis是否定义为FastLineSeries上的依赖属性
希望有帮助


Aelij.

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

<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>
System.Windows.Data错误:39: BindingExpression路径错误:

然后是您尝试绑定到的属性名称,通常最重要的是它实际尝试绑定到的类。如果这没有帮助,这里有一篇关于调试绑定的好文章:它讨论了Aelij提到的PresentationTraceSources.TraceLevel=High的使用,以及一些其他技术。希望这能走上正轨

问候,

迈克