ラジオボタンは、列挙された値を表します。この場合のテキストボックスは開いた値を表します。あなたが望むように見えるのは、開いた値のセットだけでなく、列挙された値のあらかじめ設定された選択です。これを最もよく表すコントロールはコンボボックスです。
ラジオボタン/テキストボックスアプローチを続行する場合は、列挙型フィールドの代わりに文字列フィールド/文字列型のコンバータを使用する点を除いて、ラジオボタンを列挙値にバインドする方法を変更することができます。列挙型フィールド型変換器。
列挙型にバインドする方法については、この回答を参照してください:
public class KnownStringToBooleanConverter : 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;
}
}
:How to bind RadioButtons to an enum?
は単にKnownStringToBooleanConverter
と呼ばれるクラスを作成し、文字列にこれを適応することを(これはEnumToBooleanConverter
と同じ実装であることに注意してください)また、既知の文字列で型を作成します(列挙型の作成方法に似ています):
public static class KnownCountryCodes
{
// Note: I'm guessing at these codes...
public const string England = "EN";
public const string Japan = "JP";
}
次に、同様の方法でこれにバインドします
<RadioButton IsChecked="{Binding Path=SelectedCountryCode, Converter={StaticResource KnownStringToBooleanConverter}, ConverterParameter={x:Static local:KnownCountryCodes.England}}" />
<RadioButton IsChecked="{Binding Path=SelectedCountryCode, Converter={StaticResource KnownStringToBooleanConverter}, ConverterParameter={x:Static local:KnownCountryCodes.Japan}}" />
あなたは、クロス移入するために、すべてのコントロールをしたい場合は、あなたのビューモデルにINotifyPropertyChanged
を実装する必要があります:
public class MyViewModel : INotifyPropertyChanged
{
// Bind this to TextBox and radio buttons. Populate the radio buttons manually
public string SelectedCountryCode
{
get
{
return selectedCountryCode;
}
set
{
selectedCountryCode = value;
RaiseNotifyPropertyChanged("SelectedCountryCode");
}
}
/* Todo: Implement NotifyPropertyChanged and RaiseNotifyPropertyChanged here */
private string selectedCountryCode;
}
(リストにない)カスタム値が入力されると、ラジオボタンはすべて暗くなります。リストから値を入力すると、対応するラジオボタンが点灯します。正しいラジオボタンを選択すると、値がテキストボックスで変更されます。
このView/ViewModelのものはMVVMと呼ばれます。 参照:http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
同じことを制御するラジオボタンとテキストボックスが必要なのはなぜですか?ラジオボタンのリストに表示されない値を持つことは許可されていますか? –