Reputation: 6476
I have a custom control that derives from ToggleButton
and I want to wrap a DP on the base class.
Do I need to do this...
public new bool IsEnabled
{
get { return (bool)GetValue(IsEnabledProperty); }
set
{
SetValue(IsEnabledProperty, value);
if (value && IsChecked == null)
IsChecked = false;
}
}
or is this ok...
public new bool IsEnabled
{
get { return base.IsEnabled; }
set
{
base.IsEnabled = value;
if (value && IsChecked == null)
IsChecked = false;
}
}
It seems to work the same both ways but I don't know if there are any hidden problems.
Upvotes: 0
Views: 252
Reputation: 128146
You must not call anything else than GetValue
and SetValue
in the CLR wrapper of a dependency property. The reason is explained in the XAML Loading and Dependency Properties article on MSDN.
Instead of adding a new CLR wrapper, you could call AddOwner
on the base class dependency property field to register another PropertyChangedCallback
:
public class MyToggleButton : ToggleButton
{
static MyToggleButton()
{
IsEnabledProperty.AddOwner(typeof(MyToggleButton),
new FrameworkPropertyMetadata(IsEnabledPropertyChanged));
}
private static void IsEnabledPropertyChanged(
DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var myToggleButton = (MyToggleButton)o;
if ((bool)e.NewValue && !myToggleButton.IsChecked.HasValue)
{
myToggleButton.IsChecked = false;
}
}
}
Upvotes: 1