【问题标题】:WPF ComboBox: Bind SelectedItem to a Class-MemberWPF ComboBox:将 SelectedItem 绑定到类成员
【发布时间】:2019-10-01 06:23:06
【问题描述】:

我有一个带有 Id (Int32) 和 Name (String) 的类 Person。 Persons 集合的名称显示在 ComboBox (DisplayMemberPath="Name") 中。我想将所选项目的 Id 绑定到视图模型中的属性 Int32 SelectedId。

我试过 SelectedValue="{Binding Path=SelectedId, Mode=TwoWay}" 和 SelectedValuePath="{Binding Path=SelectedId, Mode=TwoWay}" 都不起作用。

<ComboBox Name="cmbPersons"
    ItemsSource="{Binding Source={StaticResource vm},Path=Persons}"
    DisplayMemberPath="Name"
    SelectedValue="Id"
    SelectedValue="{Binding Path=SelectedId, Mode=TwoWay}"
/>

【问题讨论】:

  • 我看到我写例子的时候出错了。 ID 是 Int32。但无论如何它都不起作用。
  • 对不起,但我认为关注@Mathivanan 是有意义的。

标签: c# wpf xaml combobox binding


【解决方案1】:

试试这个。

SelectedValue="{Binding SelectedId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="Id"

更新:

我准备了一个运行良好的样本。我希望它能澄清你。 (自己实现INotifyPropertyChanged)

MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    public ObservableCollection<Person> Persons { get; set; }
    public int SelectedId { get; set; }
    public MainWindow()
    {
        InitializeComponent();

        DataContext = this;

        Persons = new ObservableCollection<Person>
        {
            new Person { Id = 1, Name = "Raj" },
            new Person { Id = 2, Name = "Ram" }
        };
    }
}

Person.cs:

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

MainWindow.xaml:

   <ComboBox Width="100" Height="30" ItemsSource="{Binding Persons}" DisplayMemberPath="Name" SelectedValue="{Binding SelectedId}" SelectedValuePath="Id"/>

【讨论】:

  • 请注意,这里不需要设置Mode=TwoWayUpdateSourceTrigger=PropertyChanged,因为两者都已经是 SelectedValue 属性的默认值。
  • 我刚刚在上面的示例中看到我犯了一个错误: SelectedValue="Id" 应该是 SelectedValuePath="Id" ,如您的示例中所示。但无论如何它都不起作用。
  • 你的 Persons 收藏是什么?
  • Persons 集合是一个 ObservableCollection。当我更改 ComboBox 的项目时,不会调用 SelectedId Setter。如果我将 SelectedId 的数据类型从 Int32 更改为 Person 类并使用 SelectedItem 而不是 SelectedValuePath 和 Selected Value,它就可以工作。
  • 您是否在所有相关属性上实现了 onproperty 更改?
【解决方案2】:

如果主要是关于获取所选项目而不是关于绑定。

您可以在不将其绑定到额外属性的情况下获取所选项目。当另一个项目被选中时,您可以使用ICollectionView 在视图模型中获取一个事件。

视图模型代码

List<PersonVm> personVms;
private ICollectionView _persons;

public ICollectionView Persons
{
    get => _persons;
    set
    {
        if (Equals(value, _persons)) return;
        _persons = value;
        OnPropertyChanged();
    }
}

// call this method in the constructor of the viewmodel
private void Init(){
    // TODO You have to add items to personVms before creating the collection

    Persons = new CollectionView(personVms);

    // Event when another item gets selected
    Persons.CurrentChanged += PersonsOnCurrentChanged;

    // moves selected index to postion 2
    Persons.MoveCurrentToPosition(2); 
}

private void PersonsOnCurrentChanged(object sender, EventArgs e)
{
    // the currently selected item
    PersonVm personVm = (PersonVm)Persons.CurrentItem;

    // the currently selected index
    int personsCurrentPosition = Persons.CurrentPosition;
}

XAML

<ComboBox 
    ItemsSource="{Binding Persons}" 
    DisplayMemberPath="Name"/>

【讨论】:

    猜你喜欢
    • 2010-10-24
    • 2021-10-18
    • 2011-11-01
    • 2015-06-28
    • 1970-01-01
    • 2017-11-09
    • 2014-10-10
    • 2016-03-25
    • 2012-06-23
    相关资源
    最近更新 更多