如何在一个嵌套在HierarchicalDataTemplate中的DataTemplate中设置RelativeSource? [英] How to set the RelativeSource in a DataTemplate that is nested in a HierarchicalDataTemplate?

查看:131
本文介绍了如何在一个嵌套在HierarchicalDataTemplate中的DataTemplate中设置RelativeSource?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下XAML,除了在检索用户之后,FontSize上的MultiBinding失败(我在转换器中设置断点时,我看到DependencyProperty.UnsetData),所以它应该做到这一点。您可以看到用户是一个 IEnumerable< UserData> ,它是HierarchicalDataTemplate的DataContext的一部分。


我如何引用?

I have the following XAML, that does all that it is supposed to, except that the MultiBinding on the FontSize fails on retrieving the Users (I see DependencyProperty.UnsetData when I set a breakpoint in the converter). As you can see Users is an IEnumerable<UserData> that is part of the HierarchicalDataTemplate's DataContext.

How do I reference it??

<TreeView Name="AllGroups" ItemsSource="{Binding}" >
      <TreeView.Resources>
           <HierarchicalDataTemplate DataType="{x:Type PrivateMessengerUI:GroupContainer}"
                                     ItemsSource="{Binding Users}"
                                                  >
                  <Label Content="{Binding GroupName}"/>
            </HierarchicalDataTemplate>
            <DataTemplate DataType="{x:Type PrivateMessenger:UserData}">
               <TextBlock Text="{Binding Username}"
                          ToolTip="{StaticResource UserDataGroupBox}"
                          Name="GroupedUser"
                          MouseDown="GroupedUser_MouseDown">
                    <TextBlock.FontSize>
                         <MultiBinding Converter="{StaticResource LargeWhenIAmSelected}">
                               <Binding ElementName="Root" Path="SelectedUser"/>
                               <Binding RelativeSource="???"
                                        Path="DataContext.Users"/>
                         </MultiBinding>
                     </TextBlock.FontSize>
               </TextBlock>
           </DataTemplate>
         </TreeView.Resources>
      </TreeView>


推荐答案

正确的答案是:

<Binding RelativeSource="{RelativeSource FindAncestor,
                                         AncestorType={x:Type TreeViewItem},
                                         AncestorLevel=2}"
                           Path="DataContext.Users"/>

祖先级别是至关重要的和欺骗性的:当您省略它时,假设1级,指向DataTemplate(这是一个没有孩子的TreeViewItem!)的容器,而不是 HierarchicalDataTemplate的容器。

The ancestorlevel is crucial and deceptive: when you omit it, a level of 1 is assumed and that actually points to the container of the DataTemplate (which is a childless TreeViewItem too!), not the container of the HierarchicalDataTemplate.

这篇关于如何在一个嵌套在HierarchicalDataTemplate中的DataTemplate中设置RelativeSource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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