XAML을 사용하여 컨트롤의 중첩 속성 (속성 값의 속성)을 설정할 수 있습니까?

StackOverflow https://stackoverflow.com/questions/834391

문제

읽기 전용 속성을 통해 어린이 중 하나 (ControlTemplate에서)를 노출시키는 WPF 컨트롤이 있습니다. 현재 그것은 단지 CLR 속성 일 뿐이지 만, 그것이 어떤 차이도 있다고 생각하지 않습니다.

나는 주요 통제를 인스턴스화하는 XAML에서 아동 통제에 대한 속성 중 하나를 설정할 수 있기를 원합니다. (사실, 나는 그것에 묶고 싶지만, 그것이 좋은 첫 단계라고 생각합니다.)

코드는 다음과 같습니다.

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
{
    // This is the property I want to set
    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); }
    }
}

다음은 중첩 된 속성을 XAML (Compile)로 설정하는 것을 생각할 수있는 두 가지 방법입니다.

<!-- 
    This doesn't work:
    "The property 'XAxis.Maximum' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'."
    "The attachable property 'Maximum' was not found in type 'XAxis'."
-->
<local:ChartControl XAxis.Maximum="{Binding Maximum}"/>

<!-- 
    This doesn't work: 
    "Cannot set properties on property elements."
-->
<local:ChartControl>
    <local:ChartControl.XAxis Maximum="{Binding Maximum}"/>
</local:ChartControl>

이것이 가능합니까?

그것 없이는 템플릿에서 어린이들에게 묶인 주요 제어에 DP를 노출해야 할 것 같습니다. 그렇게 나쁘지는 않지만, 나는 단지 주요 제어에서 속성의 폭발을 피하려고 노력하고있었습니다.

건배.

도움이 되었습니까?

해결책

이렇게 할 수는 없습니다 ... 바인딩의 경로를 통해 중첩 속성에 액세스 할 수는 있지만 속성 값을 정의 할 때는 아닙니다.

당신은 그런 일을해야합니다.

<local:ChartControl>
    <local:ChartControl.XAxis>
        <local:Axis Maximum="{Binding Maximum}"/>
    </local:ChartControl.XAxis>
</local:ChartControl>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top