食草仙 发表于 2023-4-10 15:31:36

wpf RelativeSource绑定

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>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid> 
c.TemplatedParent

TemplatedParent是一个属性,它使您能够创建一个包含少量未知值的控件模板。这些值取决于应用ControlTemplate的控件的属性。
<Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid><Grid>
    <Ellipse Width="{Binding RelativeSource={RelativeSource Self}, Path=Height}"
             Height="100"
             Fill="Black" />
</Grid> 
d.PreviousData

PreviousData这个用得很少,表示值相对于以前数据的变化。
 
 
文章导航
[*]https://github.com/aierong/WpfDemo (自我Demo地址)
 
A.Sql Server2005 Transact-SQL 新兵器学习
B.MCAD学习
C.代码阅读总结
D.ASP.NET状态管理
E.DB(数据库)
F.WAP
G.WinForm
H.Flex

希望上面提到的知识对您有所提示,同时欢迎交流和指正
作者:aierong
出处:http://www.cnblogs.com/aierong
贴子以"现状"提供且没有任何担保,同时也没有授予任何权利!
本文版权归作者所有,欢迎转载!
原创技术文章和心得,转载注明出处!这也是对原创者的尊重!


来源:https://www.cnblogs.com/aierong/archive/2023/04/10/17303019.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: wpf RelativeSource绑定