Winrt Dependency Property Visual Studio XAML error.
Asked Answered
S

1

8

This is my Dependency property:

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

I then use it like this:

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

This works perfectly at design time and run time.

However if I define it inside a control template like this:

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

It works at runtime but not in the Visual Studio designer

Sinuosity answered 1/8, 2016 at 16:50 Comment(7)
Just a question: Why are you even placing another button in Button's ControlTemplate?Communicable
Why do you care it doesn't work in the designer?Appalachian
@Communicable it happens inside any control template.Sinuosity
@Appalachian because I do. It's annoying.Sinuosity
What happens exactly? do you expect the sound to be played? how do you trigger it in design time? are you getting an error in the designer?Appalachian
Yes I expect a sound to play... and it does play. It just doesn't play when it is inside a control template. The designer displays the following error message: The text associated with this error code could not be found. Failed to assign to property ButtonTapSound.Sinuosity
It might help if you could provide some more details of your ButtonTapSoundProperty-Class, so we can try to reproduce it. Everything else may lead to various or no resultsCommunicable
N
2

Due to the lack of further source code I just can refer to the implemenation guidelines of msdn about dependency properties.

Create a seperate class for your sound button which derives from Button e.g "SoundButton" and register your property with getter and 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
    }
}

Then you can use it in your code as it is, without registering the dependecy property in xaml:

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

This is may not be done in your style, but should solve the designer issues.

Nassi answered 22/9, 2016 at 7:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.