如何将视图模型属性绑定到ListBox.SelectedItem属性?
How do I bind a view model property to the ListBox.SelectedItem property?
我已经创建了一个简单的MVVM演示,以试图找出这一个。我的视图模型具有以下属性:
I have created a simple MVVM demo to try to figure this one out. My view model has these properties:
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"); } }我想将SelectedItem属性绑定到所选项目在列表框中。这是列表框的XAML:
I want to bind the SelectedItem property to the item selected in the list box. Here is the XAML for the list box:
<ListBox ItemTemplate="{StaticResource MasterTemplate}" ItemsSource="{Binding Path=DisneyCharacters}" SelectedItem="{Binding Path=Selectedtem, Mode=TwoWay}" HorizontalAlignment="Stretch" />这是我的问题:当我更改选择时,视图模型SelectedItem属性没有被更新列表框。
Here is my problem: The view model SelectedItem property isn't being updated when I change the selection in the list box.
我做了一个测试,我暂时用SelectedIndex属性替换了ViewItem属性,并将其绑定到ListBox.SelectedIndex属性。该属性更新了 - 它只是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.
那么如何修复SelectedItem绑定?感谢您的帮助。
So, how do I fix the SelectedItem binding? Thanks for your help.
推荐答案嗯,那是一个很大的生活。在XAML中。我对一个视图模型属性Selectedtem有约束力。不幸的是,实际的名字是SelectedItem。所以这段代码实际上是有效的 - 我今天下午早些时候解决了这个问题,然后在下午剩下的时间里,整个晚上都在刷新网页,之后我注意到了拼写错误。
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.
我的妻子今天下午3点告诉我,你知道,这将是一个小小的事情。所以它是 - 一个失踪的信我。那么,至少我现在可以睡觉了。
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.
更多推荐
MVVM:绑定到ListBox.SelectedItem?
发布评论