Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I blogged about it here. My Chart control stacks the yaxes in the chart on top of each other. 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. I don't know how to assign the proper xAxis and yAxis controls to it.
Below you see part of the LineSeriesViewModel. It has a nested XAxisViewModel and YAxisViewModel property.

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. The xaml code looks like this:

<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. How can I bind the nested viewmodels to the properties of a control?

like image 435
Robert Avatar asked Nov 06 '22 19:11

Robert


2 Answers

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? Try putting a breakpoint in the 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.
  • Use PresentationTraceSources.TraceLevel=High on the Binding to get more verbose tracing (that will appear in the VS Output window).
  • Are DependentAxis/IndependentAxis defined as dependency properties on FastLineSeries?

Hope that helps,

Aelij.

like image 178
Eli Arbel Avatar answered Nov 14 '22 00:11

Eli Arbel


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. I usually end up discovering I need to explicitly set a DataContext or something else like a typo. The output to look for start like this:

System.Windows.Data Error: 39 : BindingExpression path error:

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. Hope this gets onto the right track.

Regards,

Mike

like image 39
Brosten Consulting Avatar answered Nov 14 '22 00:11

Brosten Consulting