9

読み取り専用プロパティを介して(ControlTemplateから)その子の1つを公開するWPFコントロールがあります。現時点では、これは単なるCLRプロパティですが、違いはないと思います。

メインコントロールをインスタンス化しているXAMLから子コントロールのプロパティの1つを設定できるようにしたい。(実際には、それにバインドしたいのですが、設定することは良い第一歩になると思います。)

ここにいくつかのコードがあります:

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でネストされたプロパティを設定することを考えることができる2つの方法は次のとおりです(どちらもコンパイルしません)。

<!-- 
    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を公開する必要があると思います。それほど悪くはないと思いますが、メインコントロールのプロパティの爆発を避けようとしていました。

乾杯。

4

1 に答える 1

5

このようにすることはできません...バインディング内のパスを介してネストされたプロパティにアクセスできますが、プロパティの値を定義する場合はアクセスできません。

あなたはそのようなことをしなければなりません:

<local:ChartControl>
    <local:ChartControl.XAxis>
        <local:Axis Maximum="{Binding Maximum}"/>
    </local:ChartControl.XAxis>
</local:ChartControl>
于 2009-05-07T16:50:33.907 に答える