Goal: I'm trying to bind the Margins property of a ViewXY graph to a property in the data context of the containing lightning chart.
Setup: I have a Data Context class that has a property SampleMargin.
I have defined in the xaml a lightning chart object which contains a ViewXY object.
My confusion comes when trying to bind the ViewXY.Margins property to the SampleMargin property of the data context.
I can successfully bind the SampleMargin property to the LightningChart.Margin property. But I cannot bind the property to the LightningChart.ViewXY.Margins property.
See the code below for binding.
Succeeds: Margin="{Binding SampleMargin}" >
Fails: <lcu:ViewXY Margins="{Binding SampleMargin}">
The debug output shows:
Are my expectations incorrect? Should I be able to bind the ViewXY.Margins property to a property in the data context? If so, how can I achieve this?System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=SampleMargin; DataItem=null; target element is 'ViewXY' (HashCode=216817); target property is 'Margins' (type 'Thickness')
Data Context class:
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
namespace BindingSample
{
public class SampleContext : DependencyObject
{
public Thickness SampleMargin
{
get
{
return (Thickness)GetValue(SampleMarginProperty);
}
set
{
SetValue(SampleMarginProperty, (Object)value);
}
}
public static readonly DependencyProperty SampleMarginProperty;
static SampleContext()
{
SampleMarginProperty =
DependencyProperty.Register(
"SampleMargin",
typeof(Thickness),
typeof(SampleContext),
new PropertyMetadata(
(Object)(new Thickness( 10 )),
null
)
);
}
}
}
Window Xaml:
Code: Select all
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:lcu="http://www.arction.com/schemas/" x:Class="BindingSample.MainWindow"
xmlns:local="clr-namespace:BindingSample"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:SampleContext />
</Window.DataContext>
<Grid>
<lcu:LightningChartUltimate
Content="LightningChartUltimate"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Margin="{Binding SampleMargin}" >
<lcu:LightningChartUltimate.ViewXY>
<lcu:ViewXY Margins="{Binding SampleMargin}">
<lcu:ViewXY.AxisLayout>
<lcu:AxisLayout AutoAdjustMargins="False"/>
</lcu:ViewXY.AxisLayout>
<lcu:ViewXY.YAxes>
<lcu:AxisY/>
</lcu:ViewXY.YAxes>
<lcu:ViewXY.XAxes>
<lcu:AxisX/>
</lcu:ViewXY.XAxes>
</lcu:ViewXY>
</lcu:LightningChartUltimate.ViewXY>
</lcu:LightningChartUltimate>
</Grid>
</Window>