Question

I have often bemoaned the fact that the WPF ToggleButton does not have properties for AlternateContent and AlternateContentForeground. I'm curious if there's any advantage to creating a DependencyObject with attached properties, or deriving a custom control from ToggleButton?

My assumption is that attached properties are advantageous if they are useable on more than one control. So in my case I'm leaning towards a derived control since those properties are unique to the togglebutton.

Était-ce utile?

La solution

AttachedProperties are useful in a couple scenarios:

  1. You want to use them as attached behaviors on things that interact with another Control, like Grid.Row
  2. You want to add properties to a control but you don't want to force clients that get that behavior to be derived from your specific type. E.g. if you had a behavior that you wanted on Buttons rather than ToggleButton, then you may want to go with that approach so you could get that new property on ToggleButton and RadioButton, rather than forcing someone to derive from MyCoolButton.

For what you're describing just subclassing ToggleButton seems to make sense.

Autres conseils

Actually, this kind of styling should be done with a trigger, or using the VisualStateManager.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top