2012-04-03 17 views
1

I以下のプロパティ定義があります。添付プロパティ

public static class Extensions 
{ 
    public static readonly DependencyProperty TestProperty = DependencyProperty.RegisterAttached("Test", typeof(Storyboard), typeof(UIElement), new UIPropertyMetadata(null)); 

    public static void SetTest(UIElement element, Storyboard value) 
    { 
     element.SetValue(TestProperty, value); 
    } 

    public static Storyboard GetTest(UIElement element) 
    { 
     return (Storyboard)element.GetValue(TestProperty); 
    } 
} 

を、私は、このようなXAMLをロードしようとすると、それは、XAML

<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:Application;assembly=Application" 
    x:Name="template" 
    Height="40" Width="1460"> 
    <Grid Background="#FF000000"> 
     <TextBlock Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Left" TextAlignment="Left" FontSize="32">Example</TextBlock> 
    </Grid> 
    <UserControl.Style> 
     <Style> 
      <Setter Property="local:Extensions.Test"> 
       <Setter.Value> 
        <Storyboard> 
         <DoubleAnimation Storyboard.TargetName="template" From="270" To="360" Duration="0:0:1" Storyboard.TargetProperty="Angle" /> 
        </Storyboard> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </UserControl.Style> 
</UserControl> 

に使用してみてください(XamlReader.Load(stream);を使用して例えば)次のメッセージで例外が発生します。

'local:Extensions.Test'プロパティは有効なDependencyPropプロパティではありません'Application.Extensions'タイプのrty。プロパティーにDependencyPropertyが定義され、 'Property'接尾部で終わるメンバー・フィールドが定義されていることを確認します。オブジェクト 'System.Windows.Setter'でエラーが発生しました。

誰でもこの問題を解決できますか?

ありがとうございます!

答えて

3

所有者のタイプがExtensionsの代わりUIElement次のようになります。

public static readonly DependencyProperty TestProperty = 
    DependencyProperty.RegisterAttached(
     "Test", 
     typeof(Storyboard), 
     typeof(Extensions), // here 
     new UIPropertyMetadata(null)); 

も、メタデータなしで記述できます。

public static readonly DependencyProperty TestProperty = 
    DependencyProperty.RegisterAttached(
     "Test", 
     typeof(Storyboard), 
     typeof(Extensions)); 
+0

いいえ、彼はこれがインスタンスのDependencyPropertyにする必要はありません。クラス "Extensions"では、これは、既存のクラス 'UIElement'のインスタンスに対してAttached Propertyであり、' UserControl'から継承されることを望みます。 – Alain

+0

@Alainあなたは間違っています。所有者の型は、そのプロパティを宣言する型でなければなりません。別のタイプを添付プロパティの所有者として宣言することはできません。 [here](http://msdn.microsoft.com/en-us/library/ms597495.aspx)を読んで、downvoteの前に試してください。 – Clemens

+0

それが本当の解決策であるならば、明らかに所有者を「拡張機能」にしないでください。そうでないと彼は明らかに彼の意図であるUIElementsにプロパティを適用することができません。ただし、UIElementから継承する新しい型を作成する必要はありません。 – Alain

関連する問題