Silverlight Treeview SelectedItem TwoWay 绑定导致混合错误
我在 Silverlight 4 项目中有一个 Treeview,我想绑定到它的 SelectedItem。当我对 SelectedItem (Mode=TwoWay) 进行绑定时,它会在混合中引发错误,因为 SelectedItem 是只读的,这导致我的 XAML 无法呈现。我不想设置 SelectedItem 属性,我只想知道它何时通过 UI 交互发生变化。在 WPF 中,我只需使用 Mode=OneWayToSource 绑定其 SelectedItem,但 Silverlight 不支持该模式(据我所知)。
Treeview:
<controls:TreeView ItemsSource="{Binding Repository.MajorClasses}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" />
有没有人使用过的解决方法?有人知道为什么 Silverlight 中省略了 OneWayToSource 吗?
I have a Treeview in a Silverlight 4 project, and I want to bind to its SelectedItem. When I do a binding to SelectedItem (Mode=TwoWay) its throwing an error in blend because SelectedItem is readonly, which is causing my XAML to not render. I don't ever want to SET the SelectedItem property, I just want to know when it changes via UI interaction. In WPF, I would just bind its SelectedItem using Mode=OneWayToSource, but Silverlight does not support that mode (afaik).
Treeview :
<controls:TreeView ItemsSource="{Binding Repository.MajorClasses}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" />
Is there a workaround that anyone has used? And anyone know why OneWayToSource is omitted from Silverlight?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它确实是只读的,所以你不能这样做。您可以使用 TreeView 作为基本控件并创建 CustomTreeView 并实现可绑定的 SelectedItem。或者创建自己的行为(附加属性)。或者使用一些第三方控件(fi telerik)。
It's really readonly, so you cann't do that. You can use TreeView as base control and create CustomTreeView with implementation of bindable SelectedItem. Or create own behavior(attached property). Or use some third party control (f.i. telerik).
如果您只想在用户更改选择时通知您的虚拟机,您应该能够准确地执行您正在执行的操作(双向绑定)。
我在 Visual Studio 中进行了此操作,因此我建议从那里尝试,可能只是 Blend 的问题。在 XAML 编辑器中键入时,VS intellisense 不会建议 SelectedItem,但这并不会阻止它工作。
你的虚拟机中的绑定属性肯定是正确的类型(从外观上看是MajorClass)?
If you just want your VM to be informed when the user changes the selection, you should be able to do exactly what you are doing (a two way binding).
I have this working in Visual studio so, I suggest trying it from there, might just be a problem with Blend. VS intellisense doesn't suggest SelectedItem when typing in the XAML editor but that doesn't stop it from working.
The bound property in your VM is definately of the right type (MajorClass by the looks of it)?
您需要做的是使用交互触发器并将其绑定到
SelectedItemChanged
事件,如下所示:访问 此 了解有关行为和触发器的更多信息。希望这有帮助。
What you need to do is make use of an Interaction Trigger and bind it to the
SelectedItemChanged
event as follows:Visit this for more information about Behaviors and Triggers. Hope this helps.