0

カスタムコントロールを作成しましたが、次のようにプロパティをコンテンツにバインドできません:

<Style TargetType="control:Pie">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="control:Pie">
                    <Border
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">

                        <ContentControl Content="{Binding Path=Test}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

そして私のコントロールで

public string Test
    {
        get { return (string)GetValue(TestProperty); }
        set { SetValue(TestProperty, value); }
    }
    public static readonly DependencyProperty TestProperty =
        DependencyProperty.Register(
            "Test",
            typeof(string),
            typeof(Pie),
            new PropertyMetadata(null));

コントロールを作成すると、Testプロパティに文字列が設定されますが、ビューに何も表示されません...

4

1 に答える 1

2

TemplateBindingテンプレート化されたコントロールにバインドするために使用されるため、でのみ機能しControlTemplateます。ちょうど2行上で、Background、BorderBrush、BorderThicknessにTemplateBindingをすでに使用しています。

BindingDataContext一方、は、ユーザーから「ビジネス」関連のデータを取得するために使用されるオブジェクトであるにバインドします。コントロールがどのように機能するかとは無関係である必要があります

経験則として:、またはが設定されControlTemplateていない、で通常のバインディングを使用する場合、通常はControlTemplateまたはStyleに表示されません。RelativeSourceElementNameSource

于 2012-06-19T10:08:49.090 に答える