开发者

Radiobutton style based off of custom ToggleButton

I created a custom ToggleButton class, ToggleButtonEx, that extends ToggleButton. ToggleButtonEx contains some dependency properties that I created in order to use them in a template. I want to base the style of all of my radio buttons on the new toggle button style. Here is what I have:

The ToggeButtonEx style, which defines the template:

<Style TargetType="{x:Type local:ToggleButtonEx}">      
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type local:ToggleButtonEx}">
...

The radio button style, based off of the ToggleButtonEx style:

<Style BasedOn="{StaticResource {x:Type local:ToggleButtonEx}}" TargetType="{x:Type RadioButton}">

This does not work though. According to msdn,

"If you create a style with a TargetType property and base it on another style that also defines a TargetType property, the target type of the derived style must be the same as or be derived from the type of the base style."

Since radio button is derived from ToggleButton and not ToggleButtonEx, I can't apply the style an I need the toggle button style to be based off of ToggleButtonEx or else I can't use my custom dependency properties in the template. Is there a way to do this?

UPDATE:

Here is my requirement. Maybe someone can help with an alternate solution.

I want to display radio buttons as toggle buttons, so that they look like toggle buttons but maintain the radio button mutually exclusive functionality. This was not hard to do. I created a style targeting ToggleButton to set up my desired toggle button style and then created a new style targeting RadioButton which was based off of the toggle button style.

Now in my ToggleButton style, I change the ControlTemplate to make the button have rounded corners. I decided that I would like different buttons to have different CornerRadius', or no rounding at all. I asked how that can be done in this post:

Inheriting/overriding WPF styles

One suggestion was to create a custom ToggleButton and add a new property so that it can be used in a TemplateBinding. I tried that but it looks like if it is possible, I am not doing it right. I would like to avoid adding a new style eac开发者_开发知识库h time I need a different value of CornerRadius.


No, I don't think there's a way to do it unless maybe you derive a custom RadioButton from your ToggleButtonEx duplicating all the functionality existent in RadioButton. However I doubt that's much fun - may not even be completely possible.

However maybe we'd have some suggestions on how to solve your problem in another, slightly different way; you could try outlining the broad requirements that dictated the inclusion of a DependencyProperty? Maybe there's even a built-in feature that can help you but nobody could think of suggesting it because nobody has any idea it might help you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜