简体   繁体   English

WPF DataBinding-绑定到更改引用对象的代码隐藏变量

[英]WPF DataBinding - bind to code-behind variable which changes referenced object

What I want to do: I have a ListBox bound to an ObservableCollection<MyClass> . 我想做什么:我有一个ListBox绑定到ObservableCollection<MyClass> When I select an item, I want to display some of its properties in some labels. 选择项目时,我想在某些标签中显示其某些属性。 What are the steps here? 这里有什么步骤? I have set the content of labels to {Binding Path=PropertyName} . 我已经将标签的内容设置为{Binding Path=PropertyName} What should be their DataContext ? 他们的DataContext应该是什么?

How I was going do it: I'm keeping a private variable in my window's code-behind called MyClass selectedItem , and I would like all bindings to point to that variable and its properties. 我要怎么做:我在窗口的代码隐藏中保留了一个私有变量,称为MyClass selectedItem ,我希望所有绑定都指向该变量及其属性。 It just seems more "semantic" to me, to bind to a variable with a clear meaning within the code logic, than to a presentation element's selected item. 在我看来,绑定到代码逻辑中具有明确含义的变量比呈现元素的所选项目更“语义”。

This variable can, and will, reference a different MyClass instance at any given time (I'll change it in ListBox SelectionChanged handler). 此变量可以并且将在任何给定时间引用不同的MyClass实例(我将在ListBox SelectionChanged处理程序中对其进行更改)。 I want the bindings to reflect that - to always point to the properties of the instance currently referenced by selectedItem. 我希望绑定能够反映这一点-始终指向selectedItem当前引用的实例的属性。 Will this just work ? 这会工作吗? Will the binding always point to the object that the variable points to? 绑定是否总是指向变量所指向的对象? Or will it point to the object the variable is referencing at the moment I set up the binding? 还是会在设置绑定时指向变量所引用的对象? (the latter would be my guess) (后者将是我的猜测)

Possible alternatives I can think of: Bind to listbox's SelectedItem instead of the variable referencing it. 我可以想到的可能替代方法:绑定到列表框的SelectedItem,而不是引用它的变量。 If this is the way to go, how to make it type-safe? 如果要这样做,如何使其成为类型安全的? How to ensure the binding will know it's not working with a generic ListBoxItem, but a MyClass instance, so it can see its properties? 如何确保绑定知道它不是与通用ListBoxItem一起使用,而是与MyClass实例一起使用,以便可以看到其属性? Is this needed? 需要这个吗?

Any suggestions, or explanations why I'm being stupid, or alternative approaches from the more experienced? 有什么建议或解释为什么我会很愚蠢,还是有经验的人可以选择替代方法? Thanks! 谢谢!

I would recommend using your alternate approach, I think this is the common way to do what you want. 我建议您使用替代方法,我认为这是做您想要的事情的常用方法。 If you dont fill your ListBox in code-behind or in your XAML, the SelectedItem-property will hold the MyClass instance and not a ListBoxItem instance. 如果您没有在代码后面或XAML中填充ListBox,则SelectedItem属性将保存MyClass实例而不是ListBoxItem实例。

Then you either take the SelectedItem property of your ListBox as DataContext of your labels or you create a property of Type MyClass in your ViewModel which you bind to the SelectedItem property (Mode=TwoWay). 然后,要么将ListBox的SelectedItem属性用作标签的DataContext,要么在ViewModel中创建类型MyClass的属性,该属性绑定到SelectedItem属性(Mode = TwoWay)。 Then you can bind to its properties like so: 然后,您可以像这样绑定其属性:

<Label Text="{Binding Path=MySelectedItem.Text}"/>

In this case the DataContext of your Labels has to be the ViewModel. 在这种情况下,标签的DataContext必须是ViewModel。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM