2011-01-27 17 views
0

CollectionViewSourceからバインドされたアイテムを持つItemsControlを持っています。ItemsControl内のRadioButtonのIsCheckedプロパティをバインドします。

​​

そしてoutsite別のコントロールは:私は、対応するRibbonRadioButtonプロパティがtrueまたはfalseに設定するにisCheckedたいテキストボックスの値を変更するたびに

<TextBox Text="{Binding Path=SelectedCountryCode" /> 

私は何を達成しようとしていることです。

+0

同じことを制御するラジオボタンとテキストボックスが必要なのはなぜですか?ラジオボタンのリストに表示されない値を持つことは許可されていますか? –

答えて

0

あなたがする必要があるのは、2つのプロパティを持つViewModelを作成することです。

class MyViewModel 
{ 
    // Bind this to TextBox 
    public String SelectedCountryCode { get; set; } 

    // Bind this to ItemsControl 
    public ObservableCollection<Object> VisibleFlagsImageSourcePath { get; set; } 
} 
// Note I have omitted implementation of `INotifyPropertyChanged`. But, you will need to implement it. 

そしてSelectedCountryCodeを監視し、それが変化するたびに、VisibleFlagsImageSourcePathコレクションに適切な値を変更します。

+0

Ok、SelectedCountryCodeが "EN"に設定されていると仮定します。 ImageSource "flag_english"に対応する適切なRibbonRadioButtonを見つけてIsCheckedにするにはどうすればよいですか? – brooNo

0

ラジオボタンは、列挙された値を表します。この場合のテキストボックスは開いた値を表します。あなたが望むように見えるのは、開いた値のセットだけでなく、列挙された値のあらかじめ設定された選択です。これを最もよく表すコントロールはコンボボックスです。

ラジオボタン/テキストボックスアプローチを続行する場合は、列挙型フィールドの代わりに文字列フィールド/文字列型のコンバータを使用する点を除いて、ラジオボタンを列挙値にバインドする方法を変更することができます。列挙型フィールド型変換器。

列挙型にバインドする方法については、この回答を参照してください:

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

関連する問題