Silverlight Treeview SelectedItem TwoWay 绑定导致混合错误

发布于 2024-11-30 10:14:50 字数 503 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴者 2024-12-07 10:14:50

它确实是只读的,所以你不能这样做。您可以使用 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).

我三岁 2024-12-07 10:14:50

如果您只想在用户更改选择时通知您的虚拟机,您应该能够准确地执行您正在执行的操作(双向绑定)。

我在 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)?

情感失落者 2024-12-07 10:14:50

您需要做的是使用交互触发器并将其绑定到 SelectedItemChanged 事件,如下所示:

  <sdk:TreeView x:Name="ModuleNavigationItemWrappersTreeView" ItemsSource="{Binding ModuleNavigationItemWrappers}">
                        <sdk:TreeView.ItemTemplate>
                            <sdk:HierarchicalDataTemplate ItemsSource="{Binding Children}">
                                <StackPanel Orientation="Horizontal" Margin="0,2,0,2">
                                    <Image Source="/VanguardFinancials.Common;component/Images/icons/flag_blue.png" />
                                    <TextBlock Margin="2,0,0,0" Text="{Binding ItemDescription}"></TextBlock>
                                </StackPanel>
                            </sdk:HierarchicalDataTemplate>
                        </sdk:TreeView.ItemTemplate>
                        <interactivity:Interaction.Triggers>
                            <interactivity:EventTrigger EventName="SelectedItemChanged">
                                <interactivity:InvokeCommandAction Command="{Binding TrackSelectedModuleNavigationItemWrapper}" CommandParameter="{Binding ElementName=ModuleNavigationItemWrappersTreeView}" />
                            </interactivity:EventTrigger>
                        </interactivity:Interaction.Triggers>
                    </sdk:TreeView>

访问 了解有关行为和触发器的更多信息。希望这有帮助。

What you need to do is make use of an Interaction Trigger and bind it to the SelectedItemChangedevent as follows:

  <sdk:TreeView x:Name="ModuleNavigationItemWrappersTreeView" ItemsSource="{Binding ModuleNavigationItemWrappers}">
                        <sdk:TreeView.ItemTemplate>
                            <sdk:HierarchicalDataTemplate ItemsSource="{Binding Children}">
                                <StackPanel Orientation="Horizontal" Margin="0,2,0,2">
                                    <Image Source="/VanguardFinancials.Common;component/Images/icons/flag_blue.png" />
                                    <TextBlock Margin="2,0,0,0" Text="{Binding ItemDescription}"></TextBlock>
                                </StackPanel>
                            </sdk:HierarchicalDataTemplate>
                        </sdk:TreeView.ItemTemplate>
                        <interactivity:Interaction.Triggers>
                            <interactivity:EventTrigger EventName="SelectedItemChanged">
                                <interactivity:InvokeCommandAction Command="{Binding TrackSelectedModuleNavigationItemWrapper}" CommandParameter="{Binding ElementName=ModuleNavigationItemWrappersTreeView}" />
                            </interactivity:EventTrigger>
                        </interactivity:Interaction.Triggers>
                    </sdk:TreeView>

Visit this for more information about Behaviors and Triggers. Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文