wpf 获取 ComboBox (MVVM) 的 SelectedItem

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27957944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:50:38  来源:igfitidea点击:

Getting SelectedItem of ComboBox (MVVM)

c#wpfxamlmvvmcombobox

提问by Th1sD0t

I'd like to get the selected Item of a ComboBox using the MVVM Pattern (beginner). I've read that this could be achieved by binding the SelectedItem Property to a Property in the ViewModel.

我想使用 MVVM 模式(初学者)获取组合框的选定项目。我读过这可以通过将 SelectedItem 属性绑定到 ViewModel 中的属性来实现。

XAML:

XAML:

<ComboBox ItemsSource="{Binding RoomLockerLinkCollection}" 
        DisplayMemberPath="Room.Name" 
        SelectedItem="{Binding SelectedRoom}"/>

ViewModel:

视图模型:

public Room SelectedRoom { get; set; }

But it's not working - the only thing thats happening is a appearing red border around that ComboBox - in addition after selecting a new item in the ComboBox the "SelectedRoom" property in my VM is still null.

但它不起作用 - 唯一发生的事情是该 ComboBox 周围出现红色边框 - 此外,在 ComboBox 中选择一个新项目后,我的 VM 中的“SelectedRoom”属性仍然为空。

Edit 1 :

编辑1:

One short additional question:

一个简短的附加问题:

The binding works fine - at least for the top "category". My Wrapper-Class also contains a list of lockers.

绑定工作正常 - 至少对于顶级“类别”。我的 Wrapper-Class 还包含一个储物柜列表。

<ComboBox DataContext="{Binding SelectedItem, ElementName=_cmbRoomSelection}" ItemsSource="  {Binding LockerCollection}" DisplayMemberPath="Name" SelectedValue="{Binding SAVM.SelectedLocker, Mode=TwoWay}" />

When I check the type of the SelectedValue it's a "Locker" - fine. But the SelectedLocker-Property in my VM stays null...

当我检查 SelectedValue 的类型时,它是一个“储物柜” - 很好。但是我的 VM 中的 SelectedLocker-Property 保持为空...

Additional, could s.o. explain when to use "SelectedItem" and "SelectedValue"? What's the difference? Setting the DataContext in the xaml code above can not be done by binding the SelectedValue...

另外,能否解释一下何时使用“SelectedItem”和“SelectedValue”?有什么不同?上面的xaml代码中设置DataContext是不能通过绑定SelectedValue...

Edit 2 (Solution) :

编辑 2(解决方案):

Okay, got it!

好,知道了!

As I figured out I've reset my DataContext - now the Property SAVM of course could not be found.

正如我发现的那样,我已经重置了我的 DataContext - 现在当然找不到 Property SAVM。

Solution:

解决方案:

<ComboBox DataContext="{Binding SelectedItem, ElementName=_cmbRoomSelection}" 
ItemsSource="{Binding LockerCollection}" 
DisplayMemberPath="Name" 
SelectedValue="{Binding SAVM.SelectedLocker **ElementName=_vStorage**, Mode=TwoWay}" />

回答by eran otzap

The red box is an indication of a validation error from your Binding , The most common error would be that the BindingSource and the BindingTarget are not of the same type.

红色框表示来自 Binding 的验证错误,最常见的错误是 BindingSource 和 BindingTarget 的类型不同。

Use SelectedValue and SelectedValuePath to bind to your Room object.

使用 SelectedValue 和 SelectedValuePath 绑定到您的 Room 对象。

CS :

CS :

public class Room
{
    public string RoomName { get; set; }
}

public class RoomWrapper
{
    public Room Room { get; set; }
}

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this; 
    }


    public List<RoomWrapper> RoomWrappers
    {
        get
        {
            var list = new List<RoomWrapper>();
            for (int i = 0; i < 10; i++)
            {
                list.Add(new RoomWrapper { Room = new Room { RoomName = "Room " + i } });    
            }

            return list;
        }
    }

    private Room selectedRoom;
    public Room SelectedRoom
    {
        get { return selectedRoom; }
        set
        {
            selectedRoom = value;
        }
    }

XAML :

XAML:

    <ComboBox ItemsSource="{Binding RoomWrappers}" 
           DisplayMemberPath="Room.RoomName"
           SelectedValuePath="Room" 
           SelectedValue="{Binding SelectedRoom, Mode=TwoWay}" />