How to find out which button is selected?

<GroupBox x:Name="groupBox" Header="Operating System" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="74" Width="280">
        <StackPanel>
            <RadioButton GroupName="Os" Content="Windows 7 (64-bit)" IsChecked="True"/>
            <RadioButton GroupName="Os" Content="Windows 7 (32-bit)" />
        </StackPanel>
    </GroupBox>

I have several radio button groups in my application

How can I access which one has been tested in Code-Behind using C #?

Do you absolutely need to use x: Name = on every RadioButton or is there a better way?

Code samples are always evaluated.

+4
source share
1 answer

Yes! There is a better way called binding. Out of bounds, you're pretty much stuck (I can imagine how to handle all the marked events separately and assign an enumeration, but is this really better?)

For switches, you usually use enumto represent all possible values:

public enum OsTypes
{
    Windows7_32,
    Windows7_64
}

"" . ValueEqualsConverter:

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.Equals(parameter);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((bool)value) ? parameter : Binding.DoNothing;
    }

:

<RadioButton Content="Windows 7 32-bit"
             IsChecked= "{Binding CurrentOs, 
                         Converter={StaticResource ValueEqualsConverter},
                         ConverterParameter={x:Static local:OsTypes.Windows7_32}}"

, :

public OsTypes CurrentOs {get; set;}

x: , switch - . , . MVVM WPF, !

+5

Source: https://habr.com/ru/post/1589274/


All Articles