I have a WPF control that exposes one of its children (from it a ControlTemplate) through a read-only property. At the moment this is just a CLR property, but I don't think it matters.
I want to be able to set one of the properties of a child control from XAML, where I create the main control. (Actually, I would like to get attached to it, but I think setting this up would be a good first step.)
Here is the code:
public class ChartControl : Control
{
public IAxis XAxis { get; private set; }
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.XAxis = GetTemplateChild("PART_XAxis") as IAxis;
}
}
public interface IAxis
{
double Maximum { get; set; }
}
public class Axis : FrameworkElement, IAxis
{
public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(double), typeof(Axis), new FrameworkPropertyMetadata(20.0, FrameworkPropertyMetadataOptions.AffectsRender, OnAxisPropertyChanged));
public double Maximum
{
get { return (double)GetValue(MaximumProperty); }
set { SetValue(MaximumProperty, value); }
}
}
Here are two ways I can imagine to set a nested property in XAML (nor compile):
<local:ChartControl XAxis.Maximum="{Binding Maximum}"/>
<local:ChartControl>
<local:ChartControl.XAxis Maximum="{Binding Maximum}"/>
</local:ChartControl>
Is it possible?
, , DP , ( ). , , .
.