C# 将组合框项绑定到单选按钮列表wpf的属性

C# 将组合框项绑定到单选按钮列表wpf的属性,c#,wpf,mvvm,combobox,binding,C#,Wpf,Mvvm,Combobox,Binding,我有一个组合框,我想绑定到一个可观的集合。但是,我希望在组合框中列出工具提示属性,而不是实际的单选按钮 例如: 如果我有3个单选按钮,带有工具提示1、2和3。我希望组合框包含3个字符串项,1、2和3 代码: 视图: 视图模型: 使用上面的代码,我可以看到上面提到的1、2和3项,但我无法选择它们。我猜是因为它们不是常规项目,而是单选按钮,所以单击它们会使它们处于选中/未选中状态,而不是选中状态 我可以使用额外的Strruct/类来实现我所需要的,但如果有其他方法,我当然不愿意 那么,还有其他方法吗

我有一个组合框,我想绑定到一个可观的集合。但是,我希望在组合框中列出工具提示属性,而不是实际的单选按钮

例如:

如果我有3个单选按钮,带有工具提示1、2和3。我希望组合框包含3个字符串项,1、2和3

代码:

视图:

视图模型:

使用上面的代码,我可以看到上面提到的1、2和3项,但我无法选择它们。我猜是因为它们不是常规项目,而是单选按钮,所以单击它们会使它们处于选中/未选中状态,而不是选中状态

我可以使用额外的Strruct/类来实现我所需要的,但如果有其他方法,我当然不愿意


那么,还有其他方法吗?

这里的问题不是选择,而是RadioButton是一个ContentControl。这意味着在组合框中选择时将显示其内容

您可以通过定义自定义ControlTemplate来解决此问题:

在视图模型中定义ObservableCollection会破坏MVVM模式,但我想这是有原因的。

删除MVVM标记,因为ViewModel不应该知道UI元素。相反,如果您想查看工具提示,请创建字符串列表并从单选按钮填充它。
<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();
    }
}
<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>