0

I would like to create a set of custom controls that are basically image buttons (it's a little more complex than that, but that's the underlying effect I'm going for) that I've seen a few different examples for. However, I would like to further extend that to also allow radio/toggle buttons.

What I'd like to do is have a common abstract class called ImageButtonBase that has default implementations for ImageSource and Text, etc. That makes a regular ImageButton implementation pretty easy.

The issue I am having is creating the RadioButton flavor of it. As I see it, there are at least three options:

  1. It would be easy to create something that derives from RadioButton, but then I can't use the abstract class I've created.
  2. I could change the abstract class to an interface, but then I lose the abstract implementations, and will in fact have duplication of code.
  3. I could derive from my abstract class, and re-implement the RadioButton-type properties and events (IsChecked, GroupName, etc.), but that certainly doesn't seem like a great idea.

Note: I have seen How to get a group of toggle buttons to act like radio buttons in WPF?, but what I want to do is a little more complex.

I'm just wondering if anybody has an example of an implementation, or something that might be adapted to this kind of scenario. I can see pros and cons of each of the ideas above, but each comes with potential pitfalls.

Thanks, wTs

Community
  • 1
  • 1
Wonko the Sane
  • 10,623
  • 8
  • 67
  • 92
  • To me it's confusing, you want to create a single control, which can be a button (associated with some command?), a toggle button (bound to some bool?) or a radio button (bound to some integral type)? I don't think there is ONE solution to get them all without introducing too much if-then-else-behavior-deviations and other code smells. If so, you are probably better off creating a separate class for each purpose. You could still let them extend some marker-interface, if you want to give them some common properties. – Simon D. May 10 '10 at 21:10
  • No, its more that I want multiple controls based on a single abstract class. The framework does the same thing - there is the ButtonBase control, which is inherited by the Button and ToggleButton controls. RadioButton then derives from ToggleButton. I want to create an abstract class derived from ButtonBase, with my additional properties. My Button class and RadioButton classes would derive from this. The thing I'm trying to avoid, if possible, is to redefine all the RadioButton properties and events that I would get "for free." – Wonko the Sane May 11 '10 at 03:34

1 Answers1

1

I think your best solution here is to use attached properties instead of subclassing. This will allow you to have your cake and eat it too.

Instead of writing this:

<my:CustomButton ImageSource="Abc" Text="Def" ... />

you would write this:

<Button my:ButtonLook.ImageSource="Abc" my:ButtonLook.Text="Def" ... />

This will work with data binding and everything. To implement this, create your "ButtonLook" class deriving from DependencyObject, and create the two attached properties using the "propa" snippet in Visual Studio. Then set a PropertyChangedCallback on each to construct update the ContentControl.Content property on whatever they are attached to.

An alternative solution is to embed a hidden RadioButton inside your CustomRadioButton subclass, give it an empty template to make it invisible, and bind the IsChecked and GroupName properties:

<ControlTemplate TargetType="my:CustomRadioButton">
  <Grid>
    <RadioButton IsChecked="{Binding ToggleButton.IsChecked, RelativeSource={RelativeSource TemplatedParent}}"
                 GroupName="{TemplateBinding GroupName}">
      <RadioButton.Template><ControlTemplate /></RadioButton.Template>
    </RadioButton>
    ... visual part here ...

Be sure that your CustomToggleButton and CustomRadioButton subclasses use AddOwner to create DependencyProperties such as IsChecked and GroupName rather than creating new ones.

Ray Burns
  • 62,163
  • 12
  • 140
  • 141
  • I am trying the attached properties route, but I am unclear on what you mean about the PropertyChangedHandler. The only other "issue" I have with this solution is that I'd still like to wrap these into a custom (or user) control, so it can be reused more readily (my actual abstract class has many more properties, and this would quickly get messy when added to many controls throughout the project. Of course, I could create properties in a custom control and bind them to the attached properties, but that just gets me back in the same situation as my original problem. – Wonko the Sane May 14 '10 at 20:40
  • I meant PropertyChangedCallback. As in `DependencyProperty.RegisterAttached(..., new PropertyMetadata { PropertyChangedCallback = (obj, e) => { your_code-here; }});` I'll update the answer. But from the rest of your comment, it sounds to me that my second suggestion may be more appropriate for your needs. – Ray Burns May 14 '10 at 21:29
  • So, to make sure I'm clear, I am still going to have to define any of the RadioButton (or ToggleButton) properties, events, etc. in my custom class? – Wonko the Sane May 17 '10 at 15:09
  • Yes, if you go with my second suggestion that is what you will need to do. – Ray Burns May 17 '10 at 22:52
  • Still apparently stuck. My implementation doesn't act like a RadioButton. DepProp IsCheckedProperty = ToggleButton.IsCheckedProperty.AddOwner(typeof(CustomRadioButton)); – Wonko the Sane May 20 '10 at 18:00