如何从 ComboBox 上的 SelectedItem select 属性?

How to select property from SelectedItem on ComboBox?

首先,我尝试了很多搜索,但如果我的 Google-fu 似乎有所欠缺,请随时 link 我到另一个 article/question 会回答。

我似乎误解了 SelectedItem/SelectedValue 的用法,尽管我已经阅读了多篇文章和关于它们的问题。可能我正试图将 ComboBox 绑定硬塞进它不应该成为的东西。

public class MappedValue
{
    public string Representation
    {
        get;
        set;
    }
    public object Value
    {
        get;
        set;
    }
}

public class ParameterValue
{
    public object Value
    {
        get;
        set;
    }
}   

<ComboBox ItemsSource="{Binding Path=MappedValues}"
    DisplayMemberPath="Representation"
    SelectedItem="{Binding Path=ParameterValue.Value}"
    SelectedValue="{Binding Path=ParameterValue}"
    SelectedValuePath="Value"/>

'MappedValues'只是一个合集。

所以我要做的是从选定的 MappedValue 中提取 'Value' 属性 并将其存储在 'ParameterValue.Value'.

因此,它采用整个选定的 MappedValue 对象(与 'MappedValue.Value' 属性 相对)并将其设置为 'ParameterValue.Value'.

我是不是不了解这些组合框属性的功能,还是没有简单的方法可以使用 XAML 和 ComboBox 来做到这一点?

在我的示例中,我使用了一个 ObservableCollection 来存储 class 人物的列表。如果您实现 属性 已更改的界面,则只需将所选项目设置为代码隐藏中的属性即可。这样您就可以完全访问列表中对象的属性。 希望对您有所帮助

Xaml

<ComboBox x:Name="comboBox" 
              HorizontalAlignment="Left" 
              Margin="130,83,0,0" 
              VerticalAlignment="Top" 
              Width="120" 
              ItemsSource="{Binding collection}"
              DisplayMemberPath="firstName"
              SelectedItem="{Binding SelectedPerson}"
              SelectionChanged="comboBox_SelectionChanged"/>
    <Button x:Name="btnCheck" Content="Button" HorizontalAlignment="Left" Margin="156,129,0,0" VerticalAlignment="Top" Width="75" Click="btnCheck_Click"/>

C# 代码

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private ObservableCollection<Person> collection = new ObservableCollection<Person>();

    public ObservableCollection<Person> Collection
    {
        get { return collection; }
        set { collection = value; OnPropertyChanged(); }
    }

    private Person selectedPerson = new Person();

    public Person SelectedPerson
    {
        get { return selectedPerson; }
        set { selectedPerson = value; OnPropertyChanged(); }
    }

    public MainWindow()
    {
        InitializeComponent();
        collection = new ObservableCollection<Person>() { { new Person("Murray", "Hart")}, { new Person("John", "Clifford") } };
        comboBox.ItemsSource = collection;
    }

    private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        SelectedPerson = (Person)comboBox.SelectedItem;
    }

    protected void OnPropertyChanged([CallerMemberName] string name = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }

    private void btnCheck_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(SelectedPerson.lastName);
    }
}

public class Person
{
    public string firstName { get; set; }
    public string lastName { get; set; }

    public Person()
    {

    }

    public Person(string fname, string lname)
    {
        firstName = fname;
        lastName = lname;
    }
}