First of all, WPF also has a ToggleButton. Perhaps that might be more appropriate here.
Now your error. It is basically in the following line:
DrawCircleButton.Content = !value ? imgButtonState.Source = new BitmapImage(new Uri("on.gif", UriKind.Relative)) : imgButtonState.Source = new BitmapImage(new Uri("off.gif", UriKind.Relative));
You assign a new BitmapImage to the Content property of the Button. Since the Button's ContentPresenter can't deal with that type, it simply displays the result of ToString(). It would work if you would simply drop the assignment and write the following. The Content isn't changed, simply the source of the Image that is already the Button's Content.
imgButtonState.Source = value
? new BitmapImage(new Uri("on.gif", UriKind.Relative))
: new BitmapImage(new Uri("off.gif", UriKind.Relative));
There is however still a severe problem with the definition of your dependency property ButtonState
. If you wanted to define it like this, you would have to derive from Button and write the declaration like below:
public class MyButton : Button
{
public static readonly DependencyProperty ButtonStateProperty =
DependencyProperty.Register(
"ButtonState", typeof(bool), typeof(MyButton),
new FrameworkPropertyMetadata(ButtonStatePropertyChanged));
public bool ButtonState
{
get { return (bool)GetValue(ButtonStateProperty); }
set { SetValue(ButtonStateProperty, value); }
}
private static void ButtonStatePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
((MyButton)obj).imgButtonState.Source = (bool)e.NewValue
? new BitmapImage(new Uri("on.gif", UriKind.Relative))
: new BitmapImage(new Uri("off.gif", UriKind.Relative));
}
}
You should also not do anything else expect GetValue/SetValue in the CLR wrappers of a dependency property. Instead you should use a PropertyChangedCallback as shown above. See the Implementing the Wrapper section in Checklist for Defining a Dependency Property.
If you don't want to derive from Button, you could alternatively define ButtonState
as an attached property.