Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can further simplify the accepted answer. Instead of typing out the enums as strings in xaml and doing more work in your converter than needed, you can explicitly pass in the enum value instead of a string representation, and as CrimsonX commented, errors get thrown at compile time rather than runtime:</p> <p><H2>ConverterParameter={x:Static local:YourEnumType.Enum1}</H2></p> <pre><code>&lt;StackPanel&gt; &lt;StackPanel.Resources&gt; &lt;local:EnumToBooleanConverter x:Key="EnumToBooleanConverter" /&gt; &lt;/StackPanel.Resources&gt; &lt;RadioButton IsChecked="{Binding Path=YourEnumProperty, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:YourEnumType.Enum1}}" /&gt; &lt;RadioButton IsChecked="{Binding Path=YourEnumProperty, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:YourEnumType.Enum2}}" /&gt; &lt;/StackPanel&gt; </code></pre> <p>Then simplify the converter:</p> <pre><code>public class EnumToBooleanConverter : IValueConverter { 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 value.Equals(true) ? parameter : Binding.DoNothing; } } </code></pre> <p><H3>Note - Multiple groups of RadioButtons in same container (Feb 17 '11):</H3> In xaml, if radio buttons share the same parent container, then selecting one will de-select all other's within that container (even if they are bound to a different property). So try to keep your RadioButton's that are bound to a common property grouped together in their own container like a stack panel. In cases where your related RadioButtons cannot share a single parent container, then set the GroupName property of each RadioButton to a common value to logically group them.</p> <p><H3>Note - Enum type nested in a class (Apr 28 '11):</H3> If your enum type is nested in a class (rather than directly in the namespace), you might be able to use the '+' syntax to access the enum in XAML as stated in a (not marked) answer to the question <a href="https://stackoverflow.com/questions/5676649/unable-to-find-enum-type-for-static-reference-in-wpf">Unable to find enum type for static reference in WPF</a>:</p> <p><i>ConverterParameter={x:Static local:<b>YourClass+</b>YourNestedEnumType.Enum1}</i></p> <p>Due to this <a href="https://connect.microsoft.com/VisualStudio/feedback/details/361509/xaml-designer-cannot-handle-typename-with-nested-classes" rel="noreferrer">Microsoft Connect Issue</a>, however, the designer in VS2010 will no longer load stating <code>"Type 'local:YourClass+YourNestedEnumType' was not found."</code>, but the project does compile and run successfully. Of course, you can avoid this issue if you are able move your enum type to the namespace directly.</p> <p><H3>Edit (Dec 16 '10):</H3> Thanks to anon for suggesting returning Binding.DoNothing rather than DependencyProperty.UnsetValue.</p> <p><H3>Edit (Apr 5 '11):</H3> Simplified ConvertBack's if-else to use a Ternary Operator.</p> <p><H3>Edit (Jan 27 '12):</H3> If using Enum flags, the converter would be as follows:</p> <pre><code>public class EnumToBooleanConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return ((Enum)value).HasFlag((Enum)parameter); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return value.Equals(true) ? parameter : Binding.DoNothing; } } </code></pre> <p><H3>Edit (May 7 '15):</H3> In case of a Nullable Enum (that is <strong>not</strong> asked in the question, but can be needed in some cases, e.g. ORM returning null from DB or whenever it might make sense that in the program logic the value is not provided), remember to add an initial null check in the Convert Method and return the appropriate bool value, that is typically false (if you don't want any radio button selected), like below:</p> <pre><code> public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) { return false; // or return parameter.Equals(YourEnumType.SomeDefaultValue); } return value.Equals(parameter); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload