Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to replace RadioButton with ToggleButton ? #4373

Closed
sigmarsson opened this issue Feb 27, 2021 · 12 comments
Closed

How to replace RadioButton with ToggleButton ? #4373

sigmarsson opened this issue Feb 27, 2021 · 12 comments
Labels

Comments

@sigmarsson
Copy link

I forged this but have bugs, what is your take on ?

        <Style x:Key="MyRadioButton" TargetType="RadioButton">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="RadioButton">
                        <ToggleButton Content="{TemplateBinding Content}" IsChecked="{TemplateBinding IsChecked}" Checked="OnThemeSelect" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
                <muxc:RadioButtons Header="Select Theme:" SelectedIndex="{x:Bind ThemeIndex, Mode=OneWay}">
                    <RadioButton Content="Dark Theme" Style="{StaticResource MyRadioButton}"/>
                    <RadioButton Content="Light Theme" Style="{StaticResource MyRadioButton}"/>
                </muxc:RadioButtons>

This isn't working properly.

  1. Now you can uncheck the toggle buttons.
  2. The parent templated property value reflects only at start up. Later the radio button's IsChecked property not reflecting to the buttons'..

How would you solve these ?

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Feb 27, 2021
@sigmarsson
Copy link
Author

#4367

@sigmarsson
Copy link
Author

This is supposed to be an error in the UWP framework as well :

MicrosoftDocs/windows-dev-docs#2268

@sigmarsson
Copy link
Author

sigmarsson commented Mar 1, 2021

  1. issue can be resolved with a custom control :
    internal class MyToggleButton : ToggleButton
    {
        protected override void OnToggle()
        {
            if (IsChecked.HasValue && IsChecked.Value)
            {
                return;
            }

            base.OnToggle();
        }
    }

@StephenLPeters
Copy link
Contributor

RadioButtons doesn't actually require its children to be of type radio button. It only requires that they be toggle buttons (a base class of RadioButton) so you should just be able to do something like:

                <muxc:RadioButtons Header="Select Theme:" SelectedIndex="{x:Bind ThemeIndex, Mode=OneWay}">
                    <ToggleButton Content="Dark Theme"/>
                    <ToggleButton Content="Light Theme"/>
                </muxc:RadioButtons>

@StephenLPeters
Copy link
Contributor

check out this issue which talked about retemplating radio buttons while using toggle buttons #3759

@StephenLPeters StephenLPeters added area-RadioButtons team-Controls Issue for the Controls team and removed needs-triage Issue needs to be triaged by the area owners labels Mar 3, 2021
@sigmarsson
Copy link
Author

sigmarsson commented Mar 3, 2021

@StephenLPeters, Thanks.
In the event you directly employ ToggleButtons you still gotta be dealing with the remaing circle the RadioButtons enforces to be displayed. I opted for the the Pivot control, which is per se functioning exactly this way.

@sigmarsson
Copy link
Author

However, I still believe the TempleteBinding may have an error as it does not update the target even if Mode set to OneWay.

@sigmarsson
Copy link
Author

I must re-open this one, since applying my static style onto a PivotItem resulting in this :

image

Would you have a little time to verify the TempleteBinding issue I mentioned in my first post ? 2. point.

@sigmarsson sigmarsson reopened this Mar 3, 2021
@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Mar 3, 2021
@StephenLPeters
Copy link
Contributor

Can you give some more details on what your are trying to accomplish? I'm pretty unsure based on the discussion so far. Maybe we can recommend something simpler.

@StephenLPeters StephenLPeters added needs-author-feedback Asked author to supply more information. and removed needs-triage Issue needs to be triaged by the area owners labels Mar 5, 2021
@sigmarsson
Copy link
Author

@StephenLPeters , sure, very simple. Just 2 buttons toggling mutual each other as ;

image

These are now 2 standalone toggle buttons. my curiosity was driving me to another solution with the radio buttons, where TempleteBinding isn't working the way I was expecting.

I expected that manipulating the ThemeIndex -> will change IsChecked -> will reflect to the ToggleButton's IsChecked property.

@ghost ghost added needs-triage Issue needs to be triaged by the area owners and removed needs-author-feedback Asked author to supply more information. labels Mar 5, 2021
@StephenLPeters
Copy link
Contributor

Ah, okay. So I think that RadioButtons is actually depending on code that exists in RadioButton in system xaml to enforce the mutually exclusive nature of radio button elements. So I think you are going to have trouble retemplating RadioButtons for this scenario.

I would recommend using actual check boxes with something to manage the exclusive checked state of them. You could either attach a Checked handler on them, to uncheck the other. Or you could use a TwoWay binding and a converter (that converts true to false and false to true) to tie the IsChecked property of the two toggle buttons together.

@StephenLPeters StephenLPeters removed the needs-triage Issue needs to be triaged by the area owners label Mar 6, 2021
@sigmarsson
Copy link
Author

@StephenLPeters Thanks! That is more complicated than laying out 2 standalone buttons. I did believe I could harness the RadioButton's inner mechanism on a certain degree to maximize code reuse wherever it is possible. I was wrong but learned something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants