Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, WPF also has a <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton.aspx" rel="nofollow">ToggleButton</a>. Perhaps that might be more appropriate here.</p> <p>Now your error. It is basically in the following line:</p> <pre><code>DrawCircleButton.Content = !value ? imgButtonState.Source = new BitmapImage(new Uri("on.gif", UriKind.Relative)) : imgButtonState.Source = new BitmapImage(new Uri("off.gif", UriKind.Relative)); </code></pre> <p>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.</p> <pre><code>imgButtonState.Source = value ? new BitmapImage(new Uri("on.gif", UriKind.Relative)) : new BitmapImage(new Uri("off.gif", UriKind.Relative)); </code></pre> <p>There is however still a severe problem with the definition of your dependency property <code>ButtonState</code>. If you wanted to define it like this, you would have to derive from Button and write the declaration like below:</p> <pre><code>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)); } } </code></pre> <p>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 <em>Implementing the Wrapper</em> section in <a href="http://msdn.microsoft.com/en-us/library/ms753358.aspx#checklist" rel="nofollow">Checklist for Defining a Dependency Property</a>.</p> <p>If you don't want to derive from Button, you could alternatively define <code>ButtonState</code> as an <a href="http://msdn.microsoft.com/en-us/library/ms749011.aspx" rel="nofollow">attached property</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload