2016-08-01 11 views
8

これは私の依存関係プロパティです:Winrt依存プロパティVisual Studio XAMLエラー。

public static readonly DependencyProperty ButtonTapSoundProperty = DependencyProperty.RegisterAttached("ButtonTapSound", typeof (Uri), typeof (ButtonDependencyObject), new PropertyMetadata(default(Uri), UriChanged)); 

私は、このようにそれを使用します。

<Button buttonDependencyObject:ButtonDependencyObject.ButtonTapSound="{Binding ElementName=TapSound}" ... /> 

これは、設計時と実行時に完璧に動作します。

私はこのようなコントロールテンプレートの内部でそれを定義する場合は:

<ControlTemplate x:Name="TapSound" TargetType="Button"> 
     <Button buttonDependencyObject:ButtonDependencyObject.ButtonTapSound="{Binding ElementName=TapSound}" ... /> 
</ControlTemplate> 

それは、実行時に動作しますが、原因はさらに、ソースコードの欠如へのVisual Studioデザイナー

+0

ちょっと質問:ButtonのControlTemplateに別のボタンを配置しているのはなぜですか? – lokusking

+0

なぜデザイナーではうまくいかないのですか? – MichaelThePotato

+0

@lokuskingこれはどのコントロールテンプレート内でも起こります。 –

答えて

2

に私は参照することができませんmsdnの実装ガイドラインdependency propertiesについて説明します。

「SoundButton」などButtonから派生したサウンドボタン用の別のクラスを作成し、プロパティをgetterおよびsetterに登録します。そのまま

class SoundButton : Button 
{ 
    public Uri ButtonTapSound 
    { 
     get { return (Uri)GetValue(ButtonTapSoundProperty); } 
     set { SetValue(ButtonTapSoundProperty, value); } 
    } 

    public static readonly DependencyProperty ButtonTapSoundProperty = 
     DependencyProperty.Register("ButtonTapSound", typeof(Uri), typeof(SoundButton), new PropertyMetadata(default(Uri), new PropertyChangedCallback(OnUriChanged))); 

    private static void OnUriChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     //Your code 
    } 
} 

その後、XAMLでdependecyプロパティを登録せずに、あなたのコードでそれを使用することができます。

<local:SoundButton ButtonTapSound="{Binding ElementName=TapSound}"></local:SoundButton> 

これはあなたのスタイルで行われていないことが、デザイナーの問題を解決する必要があります。

関連する問題