WPF binding RadioButton to enum

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.