WPF 将 Treeview 元素绑定到 UserControl

发布于 2024-11-04 23:38:12 字数 1364 浏览 0 评论 0原文

我有一个使用模型视图体系结构的树视图,每个 TreeViewItem 都有一个 windows Form 属性,当我单击一个节点时,我希望应用程序在树的右侧显示与该节点关联的表单。

如何使用绑定来实现此目的我已尝试以下操作,但未显示与 ApplicationForms 关联的用户控件。

   <ContentControl  Margin="163,5,127,5" Content="{Binding SelectedItem,ElementName=ApplicationTree}">
        <ContentControl.Resources>
            <DataTemplate DataType="{x:Type local:ApplicationViewModel}">
                <StackPanel>
                    <TextBlock Text="Displaying an A!" />

                    <ContentPresenter Name="MyContent">
                        <ContentPresenter.Content>
                            <UserControl x:Name="UserCntrl2" HorizontalAlignment="Stretch" Height="Auto" Width="Auto" Content="{Binding ApplicationForms}"/>
                        </ContentPresenter.Content>
                    </ContentPresenter>
                </StackPanel>
            </DataTemplate>

            <DataTemplate DataType="{x:Type local:ApplicationsViewModel}">
                <StackPanel>
                    <TextBlock Text="Displaying a B!" />
                    <!--<TextBlock Text="{Binding Bar}" />-->
                </StackPanel>
            </DataTemplate>
        </ContentControl.Resources>
    </ContentControl>

I have a tree view that is using the Model View architecture, each TreeViewItem has a windows Form attribute, when I click on a node I want the application to display the form associated with that node to the right hand side of the tree.

How can you achieve this using binding I have tried the following but the user control Associated with ApplicationForms doesn't get displayed.

   <ContentControl  Margin="163,5,127,5" Content="{Binding SelectedItem,ElementName=ApplicationTree}">
        <ContentControl.Resources>
            <DataTemplate DataType="{x:Type local:ApplicationViewModel}">
                <StackPanel>
                    <TextBlock Text="Displaying an A!" />

                    <ContentPresenter Name="MyContent">
                        <ContentPresenter.Content>
                            <UserControl x:Name="UserCntrl2" HorizontalAlignment="Stretch" Height="Auto" Width="Auto" Content="{Binding ApplicationForms}"/>
                        </ContentPresenter.Content>
                    </ContentPresenter>
                </StackPanel>
            </DataTemplate>

            <DataTemplate DataType="{x:Type local:ApplicationsViewModel}">
                <StackPanel>
                    <TextBlock Text="Displaying a B!" />
                    <!--<TextBlock Text="{Binding Bar}" />-->
                </StackPanel>
            </DataTemplate>
        </ContentControl.Resources>
    </ContentControl>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦亿 2024-11-11 23:38:12

由于您使用的是 MVVM,因此您也可以将这种逻辑放入视图模型中。然后,您可以将树节点的 IsSelected 属性绑定到您的视图模型,然后当 wpf 将 IsSelected 设置为 true 时(当用户选择该项目时),您可以做任何您想做的事情。

以这种方式使用视图模型是一种非常有用的模式。您的视图模型可以引用各种内容,并根据选择或扩展影响它们。您还可以采取相反的方式,让代码影响视图模型并让数据绑定更新实际控件

这是一篇关于 MVVM 和 Treeview 的非常好的文章

您还应该查看HierarchicalDataTemplate 如果您正在使用树视图

-编辑-

阅读后这个问题正确,我看到你已经在做正确的事情,即将你的主控件绑定到 SelectedItem 的树视图。我确实相信 SelectedItem 属性指向 TreeViewItem,而不是实际的 VM。也许这就是问题所在?

Since you're using MVVM, you can alternativly put that kind of logic in the view model. you can then bind the IsSelected property of tree node to your viewmodel, then when IsSelected get set to true by wpf (when the use selects the item) you can do whatever you want.

Its a very useful pattern to use view models this way. your viewmodels can have references to all kinds of stuff and affect them based on selection or expansion. You can also go the other way around and have code affect the viewmodels and let the databinding update the actual controls

Here is a pretty good article on MVVM and treeview

You should also check out the HierarchicalDataTemplate if you're working with treeviews

-edit-

After reading the question properly, i see that you're already doing the right thing, that is binding your master control to the SelectedItem of the Treeview. I do belive the SelectedItem property points to the TreeViewItem though, not the actual VM. Perhaps thats the problem?

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