简体   繁体   English

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

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

I used Microsoft's Chart Control of the WPF toolkit to write my own chart control. 我使用Microsoft的WPF工具包图表控件来编写自己的图表控件。 I blogged about it here . 我在这里写博客。 My Chart control stacks the yaxes in the chart on top of each other. 我的图表控件将图表中的yaxs堆叠在一起。 As you can read in the article this all works quite well. 你可以在文章中看到这一切都很有效。 Now I want to create a viewmodel that controls the data and axes in the chart. 现在我想创建一个控制图表中数据和轴的视图模型。 So far I'm able to add axes to the chart and show them in the chart. 到目前为止,我可以将轴添加到图表中并在图表中显示它们。 But I have a problem when I try to add the lineseries because it has one DependentAxis and one InDependentAxis property. 但是当我尝试添加lineseries时我遇到了问题,因为它有一个DependentAxis和一个InDependentAxis属性。 I don't know how to assign the proper xAxis and yAxis controls to it. 我不知道如何为它分配正确的xAxis和yAxis控件。
Below you see part of the LineSeriesViewModel. 下面你看到LineSeriesViewModel的一部分。 It has a nested XAxisViewModel and YAxisViewModel property. 它具有嵌套的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
}

The viewmodels all have their own datatemplate. 视图模型都有自己的datatemplate。 The xaml code looks like this: 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>

This code works quite well. 这段代码效果很好。 When I add axes they get drawn. 当我添加轴时,它们会被绘制出来。 But the DependentAxis and InDependentAxis of the lineseries control stay null, so the series doesn't get drawn. 但是lineseries控件的DependentAxis和InDependentAxis保持为空,因此系列不会被绘制。 How can I bind the nested viewmodels to the properties of a control? 如何将嵌套的视图模型绑定到控件的属性?

It should work. 它应该工作。 A few things you can check: 你可以检查几件事:

  • Does the Series Binding work? 系列装订是否有效? If so, try to figure out what's the difference. 如果是这样,试着弄清楚有什么区别。
  • Are you sure that the XAxis and YAxis properties actually have values? 您确定XAxis和YAxis属性实际上具有值吗? Try putting a breakpoint in the getter. 尝试在getter中放置一个断点。 If it's reached, the Binding works. 如果已达到,则绑定有效。 You can also put a converter (IValueConverter) on the Binding (that simply returns the value it receives) and place a breakpoint there. 您还可以在Binding上放置一个转换器(IValueConverter)(它只返回它接收的值)并在那里放置一个断点。
  • Use PresentationTraceSources.TraceLevel=High on the Binding to get more verbose tracing (that will appear in the VS Output window). 在绑定上使用PresentationTraceSources.TraceLevel = High可以获得更详细的跟踪(将出现在VS Output窗口中)。
  • Are DependentAxis/IndependentAxis defined as dependency properties on FastLineSeries? DependentAxis / IndependentAxis是否定义为FastLineSeries上的依赖项属性?

Hope that helps, 希望有所帮助,

Aelij. Aelij。

You've probably already checked this but I find that when I'm debugging bindings the first and easiest place to start is running a debug session from VS as the debug output tells which objects and properties are failing to bind. 您可能已经检查了这一点,但我发现当我调试绑定时,第一个也是最容易启动的地方是从VS运行调试会话,因为调试输出告诉哪些对象和属性无法绑定。 I usually end up discovering I need to explicitly set a DataContext or something else like a typo. 我通常最终发现我需要显式设置DataContext或其他类似拼写错误。 The output to look for start like this: 要查找的输出如下:

System.Windows.Data Error: 39 : BindingExpression path error: System.Windows.Data错误:39:BindingExpression路径错误:

this is followed by the property name you tried to bind to and usually most importantly the class against which its actually trying to bind. 接下来是您尝试绑定的属性名称,通常最重要的是它实际尝试绑定的类。 If this doesn't help there's a great article here on the debugging bindings: http://www.beacosta.com/blog/?p=52 which discusses the use of PresentationTraceSources.TraceLevel=High which Aelij mentioned, as well as a few other techniques. 如果这没有帮助,那么这里有关于调试绑定的好文章: http//www.beacosta.com/blog/? p = 52,它讨论了Aelij提到的PresentationTraceSources.TraceLevel = High的使用,以及很少有其他技巧。 Hope this gets onto the right track. 希望这能走上正轨。

Regards, 问候,

Mike 麦克风

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

相关问题 如何在MVC3中将嵌套的ViewModel从View绑定到Controller? - How can I bind nested ViewModels from View to Controller in MVC3? 如何绑定属于自定义控件的某些属性? - How can I bind certain properties belonging to a custom control? 如何将视图模型的属性绑定到用作项控件源的集合的项的属性 - How can I bind properties of a view model to properties of items of a collection used as source for an items control 如何将 TabControl 绑定到 ViewModel 集合? - How do I bind a TabControl to a collection of ViewModels? 如何从 Generic.xaml 中定义的资源字典绑定到自定义控件的依赖属性? - How can I bind to dependency properties of a custom control from a resource dictionary defined in Generic.xaml? 如何将对象的属性绑定到WPF控件? - How do I bind the properties of an object to a WPF control? 如何将控件绑定到列表 <string> ? - How can I bind a control to a List<string>? 如何将字段绑定到用户控件 - How can I bind a field to a user control 如何绑定到 ViewModel 的属性和 ViewModel 中集合项的属性? - How can I bind to properties of the ViewModel and properties of collection items in the ViewModel? 如何将数据绑定到嵌套的DevExpress GridView控件? - How do I Bind data to a nested DevExpress GridView control?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM