Files
DP44/Common/DTS.CommonCore/.svn/pristine/2d/2dbc7783a481e5aec63795ee1acd574c4818569d.svn-base

69 lines
3.3 KiB
Plaintext
Raw Permalink Normal View History

2026-04-17 14:55:32 -04:00
using System;
using System.Globalization;
using System.Windows.Data;
namespace DTS.Common.Converters
{
/// <summary>
/// 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.
/// </summary>
///
/// <remarks>
/// <para>
/// 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 |
/// --------------------------------------------------------------------------------------------------
/// </para>
/// </remarks>
[ValueConversion(typeof(bool?), typeof(bool))]
public class RadioButtonCheckedConverter : IValueConverter
{
/// <summary>
/// Maps a radio button to a specific value.
/// </summary>
/// <param name="value">The value produced by the binding source.</param>
/// <param name="targetType">The target output type.</param>
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the format operation.</param>
/// <returns>
/// <value>
/// <see langword="true" /> if [value] and [parameter] have same values; otherwise <see langword="false" />.
/// </value>
/// </returns>
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
}
/// <summary>
/// Maps a radio button to a specific value.
/// </summary>
/// <param name="value">The value that is produced by the binding target.</param>
/// <param name="targetType">This parameter is not used.</param>
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">This parameter is not used.</param>
/// <returns>
/// <value>
/// <see langword="true" /> if [value] and [parameter] have same values; otherwise <see langword="false" />.
/// </value>
/// </returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var param = bool.Parse(parameter.ToString());
return !((bool)value ^ param);
}
}
}