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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With