wpf MVVM:绑定到 ListBox.SelectedItem?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1384369/
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-08 20:57:44  来源:igfitidea点击:

MVVM: Binding to ListBox.SelectedItem?

wpfdata-bindingmvvm

提问by David Veeneman

How do I bind a view model property to the ListBox.SelectedItem property?

如何将视图模型属性绑定到 ListBox.SelectedItem 属性?

I have created a simple MVVM demo to try to figure this one out. My view model has these properties:

我创建了一个简单的 MVVM 演示来尝试解决这个问题。我的视图模型具有以下属性:

private ObservableCollection<DisneyCharacter> p_DisneyCharacters;
public ObservableCollection<DisneyCharacter> DisneyCharacters
{
    get { return p_DisneyCharacters; }

    set
    {
        p_DisneyCharacters = value;
        base.FirePropertyChangedEvent("DisneyCharacters");
    }
}

private DisneyCharacter p_SelectedItem;
public DisneyCharacter SelectedItem
{
    get { return p_SelectedItem; }

    set
    {
        p_SelectedItem = value;
        base.FirePropertyChangedEvent("SelectedItem");
    }
}

I want to bind the SelectedItem property to the item selected in the list box. Here is the XAML for the list box:

我想将 SelectedItem 属性绑定到列表框中选定的项目。这是列表框的 XAML:

<ListBox ItemTemplate="{StaticResource MasterTemplate}"
         ItemsSource="{Binding Path=DisneyCharacters}" 
         SelectedItem="{Binding Path=Selectedtem, Mode=TwoWay}" 
         HorizontalAlignment="Stretch" />

Here is my problem: The view model SelectedItem property isn't being updated when I change the selection in the list box.

这是我的问题:当我更改列表框中的选择时,视图模型 SelectedItem 属性没有更新。

I did a test where I temporarily replaced the view model SelectedItem property with a SelectedIndex property, and I bound that to the ListBox.SelectedIndex property. That property updated fine--it's just the SelectedItem property that I can't get to work.

我做了一个测试,我临时用 SelectedIndex 属性替换了视图模型 SelectedItem 属性,并将其绑定到 ListBox.SelectedIndex 属性。该属性更新得很好——它只是我无法开始工作的 SelectedItem 属性。

So, how do I fix the SelectedItem binding? Thanks for your help.

那么,如何修复 SelectedItem 绑定?谢谢你的帮助。

回答by David Veeneman

Well, there it is, big as life. In the XAML. I am binding to a view model property "Selectedtem". Unfortunately, the actual name is "SelectedItem". So this code actually works--I solved the problem early this afternoon and then spent the rest of the afternoon and all evening scouring the web, before I noticed the spelling error.

好吧,它就在那里,就像生命一样大。在 XAML 中。我绑定到视图模型属性“Selectedtem”。不幸的是,实际名称是“SelectedItem”。所以这段代码确实有效——我今天下午早些时候解决了这个问题,然后花了整个下午和整个晚上的时间在网上搜索,然后才发现拼写错误。

My wife told me at 3:00 this afternoon, "You know, it's going to turn out to be something small." And so it did--a missing letter "I". Well, at least I can go to bed now.

我妻子在今天下午 3:00 告诉我,“你知道,结果会是一件小事。” 所以它确实 - 一个丢失的字母“I”。好吧,至少我现在可以去睡觉了。