首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绑定中的WPF FindAncestor

绑定中的WPF FindAncestor
EN

Stack Overflow用户
提问于 2013-04-29 20:08:16
回答 1查看 27.2K关注 0票数 7

关于FindAncestor有一件特别的事情让我困惑,看看下面的例子:

代码语言:javascript
复制
<Expander.HeaderTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <Label Name="headerLabel"  Content="Show Contents" Padding="0" VerticalAlignment="Center" />
            <Button Name="headerButton" Margin="6,0,0,0" Content="Button" Padding="6,1" />
        </StackPanel>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Expander}}, Path=IsExpanded}" Value="True">
                <Setter TargetName="headerLabel" Property="Content" Value="Hide Contents" />
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</Expander.HeaderTemplate>

我使用上面的xaml来更改我的自定义扩展器标题的文本。我的问题是,当我想在绑定中使用祖先的属性时,什么时候需要显式地使用FindAncestor?因为至少在我的场景中,以下三个绑定似乎产生了相同的结果:

代码语言:javascript
复制
Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Expander}}, Path=IsExpanded}"

Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Expander}}, Path=IsExpanded}" 

Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Expander}}, Path=IsExpanded}"

我看过很多这三个的例子,这仅仅是个人品味的问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-29 20:16:21

来自the MSDN page about the RelativeSource.Mode property

如果未显式设置此属性,则设置AncestorType或AncestorType和AncestorLevel属性会将此属性值隐式锁定为FindAncestor。

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16278284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档