简体   繁体   English

WPF 将 Treeview 元素绑定到 UserControl

[英]WPF Binding Treeview element to a UserControl

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.我有一个使用 Model 视图架构的树视图,每个 TreeViewItem 都有一个 windows 表单属性,当我单击一个节点时,我希望应用程序在树的右侧显示与该节点关联的表单。

How can you achieve this using binding I have tried the following but the user control Associated with ApplicationForms doesn't get displayed.如何使用绑定来实现这一点我尝试了以下方法,但与 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>

Since you're using MVVM, you can alternativly put that kind of logic in the view model.由于您使用的是 MVVM,因此您可以交替地将这种逻辑放在 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.然后,您可以将树节点的 IsSelected 属性绑定到您的视图模型,然后当 wpf 将 IsSelected 设置为 true 时(当用户选择项目时),您可以做任何您想做的事情。

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您也可以 go 反过来,让代码影响视图模型并让数据绑定更新实际控件

Here is a pretty good article on MVVM and treeview这是一篇关于 MVVM 和 treeview 的不错的文章

You should also check out the HierarchicalDataTemplate if you're working with treeviews如果您正在使用树视图,您还应该查看HierarchicalDataTemplate

-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.正确阅读问题后,我发现您已经在做正确的事情,即将您的主控件绑定到 Treeview 的SelectedItem I do belive the SelectedItem property points to the TreeViewItem though, not the actual VM.我确实相信 SelectedItem 属性指向 TreeViewItem,而不是实际的 VM。 Perhaps thats the problem?也许这就是问题所在?

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

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