如何从ComboBox上的SelectedItem中选择属性?

时间:2016-02-10 13:39:39

标签: c# .net xaml combobox selecteditem

首先,我尝试了很多搜索,但如果我的Google-fu似乎缺乏搜索,请随时立即将我链接到另一篇将要回答的文章/问题。

我似乎误解了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没有简单的方法吗?

1 个答案:

答案 0 :(得分:1)

在我的例子中,我使用了一个ObservableCollection来存储Person类的列表。如果您实现了属性更改接口,则可以将所选项目设置为后面代码中的属性。这样,您就可以完全访问列表中对象的属性。 我希望这有帮助

的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;
    }
}
相关问题