惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
V
V2EX
PCI Perspectives
PCI Perspectives
Latest news
Latest news
博客园 - 三生石上(FineUI控件)
C
CERT Recently Published Vulnerability Notes
W
WeLiveSecurity
Last Week in AI
Last Week in AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Palo Alto Networks Blog
T
The Exploit Database - CXSecurity.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
WordPress大学
WordPress大学
V
Vulnerabilities – Threatpost
H
Heimdal Security Blog
Attack and Defense Labs
Attack and Defense Labs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
博客园 - 叶小钗
V
Visual Studio Blog
Jina AI
Jina AI
P
Proofpoint News Feed
罗磊的独立博客
SecWiki News
SecWiki News
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 热门话题
Security Archives - TechRepublic
Security Archives - TechRepublic
The Hacker News
The Hacker News
Hugging Face - Blog
Hugging Face - Blog
N
News and Events Feed by Topic
NISL@THU
NISL@THU
T
Tailwind CSS Blog
T
Tenable Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Recent Announcements
Recent Announcements
H
Hacker News: Front Page
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Tor Project blog
宝玉的分享
宝玉的分享
Help Net Security
Help Net Security
S
Security Affairs
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
G
GRAHAM CLULEY

博客园 - aierong

CommunityToolkit.Mvvm系列文章导航 CommunityToolkit.Mvvm8.1 IOC依赖注入控制反转(5) CommunityToolkit.Mvvm8.1 消息通知(4) CommunityToolkit.Mvvm8.1 viewmodel源生成器写法(3) CommunityToolkit.Mvvm8.1 viewmodel使用-旧式写法(2) CommunityToolkit.Mvvm8.1 MVVM工具包安装引用指南(1) Jquery利用ajax调用asp.net webservice的各种数据类型(总结篇) 创建Windows服务(Windows Services)N种方式总结 .NET中对串口(COM)读写操作方式汇总 Sql Server2008 Transact-SQL 新兵器学习总结之-用户定义表类型和日期,时间数据类型 Air版免费视频成人聊天室,免费网络远程视频会议系统((Flex,Fms3联合打造))<视频聊天,会议开发实例7> Flex(flash)检测摄像头的3种状态(是否被占用,没安装摄像头,正常) - aierong - 博客园 Flex Air开发SQLite小结,SQLite开发工具及SQLite与Sql Server的语法差异汇总 开源Flex Air版免费激情美女视频聊天室,免费网络远程视频会议系统((Flex,Fms3联合打造))<视频聊天,会议开发实例6> 免费网络远程视频会议系统,免费美女多人视频聊天(附源码下载)(Flex和Fms3开发)<视频聊天,会议开发实例5> Flex组件的项目渲染器(ItemRenderer)使用总结 推荐几个Adobe Flex Builder 3的插件(代码格式化和fms服务器通讯文件(main.asc)编写) Flex组件开发总结-20090209 免费美女视频聊天,多人视频会议功能加强版本(Fms3和Flex开发(附源码))<视频聊天,会议开发实例4>
wpf RelativeSource绑定
aierong · 2023-04-10 · via 博客园 - aierong

RelativeSource有四种类型

Self

FindAncestor

TemplatedParent

PreviousData

a.Self

Self用于绑定源和绑定目标相同的场景中。对象的一个属性与同一对象的另一个属性绑定。

例如,让我们取一个高度和宽度相同的椭圆。在XAML文件中添加下面给出的代码。宽度属性与高度属性相对绑定。

<Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid>

b.FindAncestor

FindAncestor
顾名思义,当绑定源是绑定目标的祖先(父级)之一时使用此选项。使用FindAncestor扩展,可以找到任何级别的祖先。
             

现在,让我们使用FindAncestor扩展将祖先的Name属性绑定到子元素button的Content属性。

<Grid Name="Parent_3">
    <StackPanel Name="Parent_222"
                Width="100"
                HorizontalAlignment="Center"
                VerticalAlignment="Center">
        <StackPanel Name="Parent_2"
                    Width="100"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center">
            <Border Name="Parent_1">
                <StackPanel x:Name="Parent_0"
                            Orientation="Vertical">
                    <!--  下面这个按钮Content得到:Parent_2  -->
                    <Button Height="50"
                            Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}, AncestorLevel=2}, Path=Name}" />
                    <!--  下面这个按钮Content得到:Parent_0  -->
                    <Button Height="50"
                            Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}, AncestorLevel=1}, Path=Name}" />
                    <!--  下面这个按钮Content得到:Parent_0  -->
                    <Button Height="50"
                            Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}, Path=Name}" />
                </StackPanel>
            </Border>
        </StackPanel>
    </StackPanel>
</Grid>

c.TemplatedParent

TemplatedParent是一个属性,它使您能够创建一个包含少量未知值的控件模板。这些值取决于应用ControlTemplate的控件的属性。

    <Window.Resources>
        <ControlTemplate x:Key="template1">
            <!--
                在应用模板时,按钮的Background(Beige)与椭圆的Fill属性相对绑定,Content(Click me)与ContentPresenter的Content属性相对绑定。依赖值生效并给出以下输出。
            -->
            <Canvas>
                <Ellipse Width="155"
                         Height="110"
                         Fill="Black" />
                <Ellipse Width="150"
                         Height="100"
                         Fill="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />
                <ContentPresenter Margin="35"
                                  Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" />
            </Canvas>
        </ControlTemplate>
    </Window.Resources>
<Button Height="0"
                        Margin="5"
                        Background="Beige"
                        Content="Click me"
                        FontSize="18"
                        Template="{StaticResource template1}" />

d.PreviousData

PreviousData这个用得很少,表示值相对于以前数据的变化。

最终效果图