DataBind to WPF MaterialRadioButton Group in CodeBehind (no XAML)
Greetings, can one bind to the selected Radio Item in code behind, please show an example if this is possible.
Hello,
You can get the selected Radio button this way: < StackPanel x:Name="_stackPanel" HorizontalAlignment="Center"> <xctk:MaterialRadioButton x:Name="materialRadioButton" GroupName="Choice" IsChecked="True" Content="First" /> <xctk:MaterialRadioButton x:Name="materialRadioButton2" GroupName="Choice" Content="Second"/> <Button Content="TEST" Click="Button_Click" /> </StackPanel>
private void Button_Click( object sender, RoutedEventArgs e ) { var checkedButton = _stackPanel.Children.OfType<MaterialRadioButton>().FirstOrDefault( r => r.GroupName == "Choice" && r.IsChecked.Value ); }
You can also register to the Checked event on MaterialRadioButton: < xctk:MaterialRadioButton x:Name="materialRadioButton" GroupName="Choice" IsChecked="True" Content="First" Checked="materialRadioButton_Checked"/> < xctk:MaterialRadioButton x:Name="materialRadioButton2" GroupName="Choice" Content="Second" Checked="materialRadioButton_Checked"/>
private void materialRadioButton_Checked( object sender, RoutedEventArgs e ) { var radioButton = sender as MaterialRadioButton; if( radioButton != null ) { var isChecked = radioButton.IsChecked; } }
You can also have a look at https://www.c-sharpcorner.com/article/explain-radio-button-binding-in-mvvm-wpf/
Remember that a MaterialRadioButton is a RadioButton. Thank you