2017-07-04 19 views
0

私はObservableCollection<RadioButton>にバインドしたいComboBoxを持っています。ただし、ではなくComboBoxToolTipプロパティを記載したいと思います。コンボボックスのアイテムをラジオボタンのリストのプロパティにバインドするwpf

例::私は3 ToolTips 1とRadioButtons、2、および3を持っている場合は

は、私は3列の項目を含むようにComboBox、1、2、3

コードを望みます

ビュー:

<ComboBox x:Name="LandmarkIdComboBox" Grid.Column="1" DisplayMemberPath="ToolTip" ItemsSource="{Binding Landmarks, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
      SelectedItem="{Binding SelectedLandmark, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalContentAlignment="Center" HorizontalAlignment="Stretch" 
      VerticalAlignment="Center" VerticalContentAlignment="Center"/> 

ビューモデル:上記のコードを使用して

private ObservableCollection<RadioButton> m_landmarks = new ObservableCollection<RadioButton>(); 
private RadioButton m_selectedLandmark; 

public ObservableCollection<RadioButton> Landmarks 
{ 
    get => m_landmarks; 
    set 
    { 
     m_landmarks = value; 
     OnPropertyChanged(); 
    } 
} 
public RadioButton SelectedLandmark 
{ 
    get => m_selectedLandmark; 
    set 
    { 
     m_selectedLandmark = value; 
     OnPropertyChanged(); 
    } 
} 

Iが挙げ1,2および3アイテムを見ることができ、私はそれを選択することができません。私は彼らが普通のアイテムではなく、RadioButtonsなので、それらをクリックすると、選択されているのではなく、チェックされている/選択されていないことになります。

別の方法がある場合は、私が必要とするものを達成するためにStrruct/Classを追加することもできますが、もちろんそうではありません。

別の方法がありますか?

+0

MVVMタグを削除すると、ViewModelはUI要素を意識するべきではありません。代わりにツールヒントを表示したい場合は、文字列のリストを作成し、 'RadioButtons'から入力してください。 – XAMlMAX

答えて

1

これは問題ではなく、RadioButtonContentControlであるという事実です。つまり、ComboBoxで選択されている場合、そのContentが表示されます。しかしMVVMパターンを破るビューモデルにObservableCollection<RadioButton>の定義

<ComboBox x:Name="LandmarkIdComboBox" Grid.Column="1" ItemsSource="{Binding Landmarks}" SelectedItem="{Binding SelectedLandmark}" 
      HorizontalContentAlignment="Center" HorizontalAlignment="Stretch" 
      VerticalAlignment="Center" VerticalContentAlignment="Center"> 
    <ComboBox.Resources> 
     <Style TargetType="RadioButton"> 
      <Setter Property="Content" Value="{Binding ToolTip, RelativeSource={RelativeSource Self}}" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="RadioButton"> 
         <TextBlock Text="{TemplateBinding ToolTip}" /> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </ComboBox.Resources> 
</ComboBox> 

が、私はあなたの理由があると思います:

カスタムControlTemplateを定義することによってこの問題を解決することができます。

関連する問題