0

依存関係プロパティを持つカスタム コントロールを開発します

public static readonly DependencyProperty StateBorderBrushProperty =
        DependencyProperty.Register("StateBorderBrush", typeof(Brush), typeof(SmartCanvas),
        new FrameworkPropertyMetadata(Brushes.Transparent,
            FrameworkPropertyMetadataOptions.None));

xaml のように外部からコントロールの ControlTemplate を設定しようとすると、問題が発生します

<ControlTemplate TargetType="controls:SmartPrimitive">
                    <Grid>
                        <ContentPresenter/>
                        <Border BorderBrush="{TemplateBinding StateBorderBrush}" BorderThickness="2"/>
                    </Grid>
                </ControlTemplate>

上記の TemplateBinding の文字列では、「XamlParseException: The given key was not present in the Dictionary」のように聞こえます。何が間違っている可能性がありますか?

4

3 に答える 3

1

{x:Type }宣言を見逃した

<ControlTemplate TargetType="{x:Type controls:SmartPrimitive}">
     <Grid>
         <ContentPresenter/>
         <Border BorderBrush="{TemplateBinding StateBorderBrush}" BorderThickness="2"/>
     </Grid>
</ControlTemplate>

これは、Type ではなく TargetType に文字列を指定していることを意味します。

x:Type マークアップ拡張機能は、Type 型を取るプロパティの文字列からの変換動作を提供します。入力は XAML 型です。

http://msdn.microsoft.com/en-us/library/ms753322%28v=vs.110%29.aspx

于 2014-11-28T15:32:32.877 に答える