Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winrt Dependency Property Visual Studio XAML error.

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

like image 292
Cool Breeze Avatar asked Aug 01 '16 16:08

Cool Breeze


1 Answers

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.

like image 174
Astasian Avatar answered Sep 29 '22 12:09

Astasian