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

推荐订阅源

Cyberwarzone
Cyberwarzone
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
The Exploit Database - CXSecurity.com
有赞技术团队
有赞技术团队
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
GbyAI
GbyAI
A
Arctic Wolf
Simon Willison's Weblog
Simon Willison's Weblog
美团技术团队
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
NISL@THU
NISL@THU
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
N
News and Events Feed by Topic
M
MIT News - Artificial intelligence
T
Tenable Blog
爱范儿
爱范儿
Stack Overflow Blog
Stack Overflow Blog
S
Security Affairs
T
Troy Hunt's Blog
月光博客
月光博客
SecWiki News
SecWiki News
PCI Perspectives
PCI Perspectives
V
V2EX
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
F
Fortinet All Blogs
IT之家
IT之家
F
Full Disclosure
博客园_首页
C
CERT Recently Published Vulnerability Notes
T
Threatpost
Last Week in AI
Last Week in AI
C
Cisco Blogs
H
Heimdal Security Blog
大猫的无限游戏
大猫的无限游戏
Webroot Blog
Webroot Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Attack and Defense Labs
Attack and Defense Labs
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
量子位
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
I
InfoQ
P
Proofpoint News Feed
Y
Y Combinator Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main

博客园 - 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这个用得很少,表示值相对于以前数据的变化。

最终效果图