using System; using System.Globalization; using System.Windows.Data; namespace DTS.Common.Converters { /// /// This converter is used to bind a value to a group of radio buttons, and can be used with bool parameters only. /// It depends on the converter parameter, which maps a radio button to a specific value. /// /// /// /// /// Notice the converter parameter. /// Its role is to make sure to get opposite values for the two radio buttons (when one is true, the other is false). /// -------------------------------------------------------------------------------------------------- /// | value | param | result : (RadioButton.IsChecked = !(value ^ param)) where ^ is the XOR operator | /// -------------------------------------------------------------------------------------------------- /// | true | true | true | /// | false | true | false | /// | true | false | false | /// | false | false | true | /// -------------------------------------------------------------------------------------------------- /// /// [ValueConversion(typeof(bool?), typeof(bool))] public class RadioButtonCheckedConverter : IValueConverter { /// /// Maps a radio button to a specific value. /// /// The value produced by the binding source. /// The target output type. /// The converter parameter to use. /// The culture to use in the format operation. /// /// /// if [value] and [parameter] have same values; otherwise . /// /// public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var param = bool.Parse(parameter.ToString()); if (value == null) return false; return !((bool)value ^ param); // ^ is the XOR operator } /// /// Maps a radio button to a specific value. /// /// The value that is produced by the binding target. /// This parameter is not used. /// The converter parameter to use. /// This parameter is not used. /// /// /// if [value] and [parameter] have same values; otherwise . /// /// public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { var param = bool.Parse(parameter.ToString()); return !((bool)value ^ param); } } }