オブジェクトenum
の値を名前として (大まかに) 含むを作成し、を使用してプロパティを this のタイプのプロパティにバインドできます。RadioButton
IsChecked
enum
EnumToBoolConverter
public enum Options
{
All, Current, Range
}
次に、ビュー モデルまたはコード ビハインドで:
private Options options = Options.All; // set your default value here
public Options Options
{
get { return options; }
set { options = value; NotifyPropertyChanged("Options"); }
}
次を追加しConverter
ます。
[ValueConversion(typeof(Enum), typeof(bool))]
public class EnumToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || parameter == null) return false;
string enumValue = value.ToString();
string targetValue = parameter.ToString();
bool outputValue = enumValue.Equals(targetValue, StringComparison.InvariantCultureIgnoreCase);
return outputValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || parameter == null) return null;
bool useValue = (bool)value;
string targetValue = parameter.ToString();
if (useValue) return Enum.Parse(targetType, targetValue);
return null;
}
}
最後に、適切な を設定して、UI にバインディングを追加しますConverterParameter
。
<RadioButton Content="All Pages" IsChecked="{Binding Options, Converter={
StaticResource EnumToBoolConverter}, ConverterParameter=All}" />
<RadioButton Content="Current Page" IsChecked="{Binding Options, Converter={
StaticResource EnumToBoolConverter}, ConverterParameter=Current}" />
<RadioButton Content="Page Range" IsChecked="{Binding Options, Converter={
StaticResource EnumToBoolConverter}, ConverterParameter=Range}" />
Options
これで、ビュー モデルまたはコード ビハインドの変数を見ることで、どちらが設定されているかがわかります。RadioButton
プロパティを設定することで、チェック済みを設定することもできOptions
ます。