2011-01-27 4 views
0

CollectionViewSource에서 바인딩 한 항목이있는 ItemsControl을 보유하고 있습니다.ItemsControl 내에서 RadioButton의 IsChecked 속성을 바인딩합니다.

<ItemsControl ItemsSource="{Binding Source={StaticResource VisibleFlagsImageSourcePathView}}"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel /> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <r:RibbonRadioButton SmallImageSource="{Binding}" /> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 

그리고 outsite 다른 컨트롤 : 내가 해당 RibbonRadioButton 속성이 true 또는 false로 설정의 IsChecked 원하는 텍스트 상자의 값을 변경할 때마다

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

내가 달성하기 위해 시도하고있다.

+0

같은 것을 제어하는 ​​라디오 버튼과 텍스트 상자가 필요한 이유는 무엇입니까? 라디오 버튼 목록에 나타나지 않는 값을 가질 수 있습니까? –

답변

0

두 가지 속성으로 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

관련 문제