您如何导航复杂的可视化树以重新绑定现有元素? [英] How do you navigate a complex Visual Tree in order to re-bind an existing element?

查看:30
本文介绍了您如何导航复杂的可视化树以重新绑定现有元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上图中,child 是 ContentPresenter.它的 Content 是一个 ViewModel.但是,它的 ContentTemplate 为空.

在我的 XAML 中,我有一个具有以下结构的 TabControl:

<DockPanel><标签面板DockPanel.Dock="顶部"IsItemsHost="True"/><网格DockPanel.Dock="底部"x:Name="PART_ItemsHolder"/></DockPanel><!-- 没有内容演示者--></控制模板></TabControl.Template><TabControl.Resources><DataTemplate DataType="{x:Type vm:WorkSpaceViewModel}">....

WorkSpaceViewModels 是 WorkSpaceViewModel 的 ObservableCollection.此代码使用了来自 防止 WPF 选项卡控件破坏其孩子.

正确的 DataTemplate - 上面显示在 TabControl.Resource 中 - 似乎正在为两个选项卡呈现我的 ViewModel.

但是,我的基本问题是,我的视图如何连接到我的 WorkSpaceViewModel,而 ContentPresenter 上的 ContentTemplate 为空?我的要求是从 ViewModel 访问可视化组件,因为在某些用户操作时,视图的设置与其在 ViewModel 中的属性解除绑定,我需要重新绑定它.

解决方案

DataTemplate 是隐式"定义的.ContentPresenter 将首先使用它的 ContentTemplate/Selector,如果有的话.如果不是,那么它将搜索没有显式 x:Key 且其 DataType 与其内容类型匹配的 DataTemplate 资源.

此处这里.

视图模型不应该真正知道它关联的视图.听起来您的绑定有问题,因为通常您不必重新绑定"它们.无论哪种方式,附加行为都是实现这一目标的好方法.>

In the above image, child is a ContentPresenter. Its Content is a ViewModel. However, its ContentTemplate is null.

In my XAML, I have a TabControl with the following structure:

<local:SuperTabControlEx DataContext="{Binding WorkSpaceListViewModel}"
 x:Name="superTabControl1" CloseButtonVisibility="Visible" TabStyle="OneNote2007" ClipToBounds="False" ContentInnerBorderBrush="Red" FontSize="24" >
            <local:SuperTabControlEx.ItemsSource>
                <Binding Path="WorkSpaceViewModels" />
            </local:SuperTabControlEx.ItemsSource>

                <TabControl.Template>
                <ControlTemplate
                    TargetType="TabControl">
                        <DockPanel>
                            <TabPanel
                            DockPanel.Dock="Top"
                            IsItemsHost="True" />
                            <Grid
                            DockPanel.Dock="Bottom"
                            x:Name="PART_ItemsHolder" />
                        </DockPanel>
                        <!-- no content presenter -->
                    </ControlTemplate>
                    </TabControl.Template>

            <TabControl.Resources>
            <DataTemplate DataType="{x:Type vm:WorkSpaceViewModel}">

            ....

WorkSpaceViewModels is an ObservableCollection of WorkSpaceViewModel. This code uses the code and technique from Keeping the WPF Tab Control from destroying its children.

The correct DataTemplate - shown above in the TabControl.Resource - appears to be rendering my ViewModel for two Tabs.

However, my basic question is, how is my view getting hooked up to my WorkSpaceViewModel, yet, the ContentTemplate on the ContentPresenter is null? My requirement is to access a visual component from the ViewModel because a setting for the view is becoming unbound from its property in the ViewModel upon certain user actions, and I need to rebind it.

解决方案

The DataTemplate is "implicitly" defined. The ContentPresenter will first use it's ContentTemplate/Selector, if any is defined. If not, then it will search for a DataTemplate resource without an explicit x:Key and whose DataType matches the type of it's Content.

This is discussed here and here.

The View Model shouldn't really know about it's associated View. It sounds like there is something wrong with your Bindings, as in general you should not have to "rebind" them. Either way, an attached behavior would be a good way to accomplish that.

这篇关于您如何导航复杂的可视化树以重新绑定现有元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆