Multiple RadioButton selections are often required in WPF, and the selected option needs to be mapped to an enum value enum.
The solution to this situation is as follows:
1) need to create a UserControl, for example code is as follows:
<usercontrol x:class= "Shangfeixapp.xmodeusercontrol"
Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns:mc= "http://schemas.openxmlformats.org/markup-compatibility/2006"
Xmlns:d= "http://schemas.microsoft.com/expression/blend/2008"
Xmlns:src= "Clr-namespace:shangfeixapp"
mc:ignorable= "D" >
<UserControl.Resources>
<src:xdatamodevalueconverter x:key= "Xmodeconverter"/>
</UserControl.Resources>
<grid x:name= "LayoutRoot" background= "white" margin= "ten" >
<Grid.RowDefinitions>
<rowdefinition height= "Auto"/>
<rowdefinition height= "Auto"/>
<rowdefinition height= "Auto"/>
</Grid.RowDefinitions>
<stackpanel orientation= "Horizontal" grid.row= "1" horizontalalignment= "Center" >
<radiobutton content= "Background" margin= "0 0 0" groupname= "mode" ischecked= "{Binding mode, Mode=twoway, Converterparameter=background_mode, Converter={staticresource Xmodeconverter}} "/>
<radiobutton content= "full scale" margin= "0 0 0" groupname= "mode" ischecked= "{Binding Mode, Mode=twoway, Converterp Arameter=fullscale_mode, Converter={staticresource Xmodeconverter}} "/>
<radiobutton content= "Normal" margin= "0 0 0" groupname= "mode" ischecked= "{Binding Mode, Mode=twoway, Converterparameter=normal_mode, Converter={staticresource Xmodeconverter}} "/>
</StackPanel>
</Grid>
</UserControl>
2) Define the enumeration and a class that carries the enumeration property, as follows:
public enum Xdatamode {background_mode, Fullscale_mode, normal_mode};
public class Xdatamodevalue:inotifypropertychanged
{
Private Xdatamode _mode = Xdatamode.background_mode;
Public Xdatamode Mode
{
get {return _mode;}
Set
{
if (_mode! = value)
{
_mode = value;
Notifypropertychanged ("mode");
}
}
}
private void Notifypropertychanged (String propertyname)
{
if (propertychanged! = null)
{
PropertyChanged (This, new PropertyChangedEventArgs (PropertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
3) define bool with the enum worthy conversion class:
[Valueconversion (typeof (Xdatamode), typeof (Boolean))]
public class Xdatamodevalueconverter:ivalueconverter
{
BOOL Turn Visibility
public object Convert (object value, Type targetType, object parameter, CultureInfo culture)
{
return (value. ToString () = = parameter. ToString ());
}
public object Convertback (object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool) value? Enum.parse (targetType, parameter. ToString (), true): null;
}
}
4) Define a variable in Mainwindows.cs or other code similar to the following:
Public Xdatamodevalue Xdatamodevalue {get; set;}
5) Implement the following source code in MainWindow, that is, given context, easy to bind
Public MainWindow ()
{
InitializeComponent ();
...
Loaded + = mainpage_loaded;
}
private void Mainpage_loaded (object sender, RoutedEventArgs e)
{
DataContext = Xdatamodevalue;
}
Complete!
WPF binding RadioButton to enum