草庐IT

wpf RelativeSource绑定

aierong原创技术随笔 2023-06-12 原文

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

 

最终效果图

 

 

文章导航
  1. https://github.com/aierong/WpfDemo (自我Demo地址)

 

有关wpf RelativeSource绑定的更多相关文章

  1. ruby-on-rails - 创建 ruby​​ 数据库时惰性符号绑定(bind)失败 - 2

    我正在尝试在Rails上安装ruby​​,到目前为止一切都已安装,但是当我尝试使用rakedb:create创建数据库时,我收到一个奇怪的错误:dyld:lazysymbolbindingfailed:Symbolnotfound:_mysql_get_client_infoReferencedfrom:/Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundleExpectedin:flatnamespacedyld:Symbolnotfound:_mysql_get_client_infoReferencedf

  2. ruby - ruby 中绑定(bind)对象的实际使用 - 2

    昨晚,我在思考我认为是高级ruby​​语言的功能,即Continuations(callcc)和Bindingobjects。我的意思是高级,因为我有静态类型的oo语言背景(C#、Java、C++),我最近才发现ruby​​,所以这些语言特性对我来说不是很熟悉。我想知道这些语言功能在现实世界中的用途是什么。根据我的经验,一切都可以用静态类型的oo语言来完成,但有时我不太同意。我想我在阅读SamRuby的那篇好文章时发现了Continuation的美妙之处/兴趣:http://www.intertwingly.net/blog/2005/04/13/Continuations-for-C

  3. ruby - svn ruby​​ 绑定(bind)是否作为 gem 提供? - 2

    我看到有几十个与svn相关的gem,但是我在其中任何一个上找到的少量文档表明它们是命令行包装器和杂项帮助程序。(svn命令、svn钩子(Hook)等)我在野外看到过执行以下操作的代码:require'svn/core'和SVN.Repos.add(...),但该模块的作者通过apt-get提取了他的svnruby​​工具。这对我来说不是一个选择,因为我正在开发一个windows/osx工具。Thispage列出了一些项目,但特别是,我需要一些可以访问svn+ssh存储库的东西,而且我没有时间花一半的时间来挖掘文档-十几个项目,试图引导每一个。我在寻找哪个gem?从那里开始,我很乐意挖掘

  4. ruby-on-rails - 如何将 rvm 与 eclipse 的设置绑定(bind) - 2

    我正在为不同的应用程序使用多个ruby​​实例和gemset进行开发。为了在我的ruby​​版本和gemset之间切换,我使用.rvmrc文件。我还使用带有radrails插件的Eclipse作为我的开发工具。有什么方法可以让eclipse使用rvm在不同的gemsets和ruby​​版本之间自动切换? 最佳答案 Gointoyourprojectdirectoryandcreateafilecalled.rvmrccontainingtheline:rvmuseruby-1.8.7-p330@testing--default(or

  5. ruby-on-rails - Rails Controller 操作是否隐式定义事务绑定(bind)? - 2

    给定以下代码:defcreate@something=Something.new(params[:something])thing=@something.thing#anothermodel#modificationofattributesonboth'something'and'thing'omitted#doIneedtowrapitinsideatransactionblock?@something.savething.saveendcreate方法是隐式包装在ActiveRecord事务中,还是需要将其包装到事务block中?如果我确实需要包装它,这是最好的方法吗?

  6. ruby - 我可以在 Ruby 中出现异常时访问绑定(bind)吗 - 2

    假设我有:begin2.timesdoa=11/0endrescueputs$!debuggerend在这个例子中,我想获取a的值。如果a在beginblock中初始化,那么我可以在救援时访问它。但是,在此示例中,a是block本地的。当我救援时,有没有办法在异常时刻获得绑定(bind)? 最佳答案 你不能在doblock中再放一个begin,rescueblock吗? 关于ruby-我可以在Ruby中出现异常时访问绑定(bind)吗,我们在StackOverflow上找到一个类似的问题

  7. ruby - 是否可以为 Ruby 创建 Crystal 绑定(bind)? - 2

    我正在创建一个m3u8用于crystal的生成器/解析器,但我想稍后将其与ruby​​一起使用。这可能/容易做到吗? 最佳答案 是的,您可以使用Crystal创建Ruby扩展。足够bindruby库。有一个有趣的presentationbyAnnaKazakowa关于那个话题。另见:phoffer/crystalized_rubymanastech/crystal_ruby 关于ruby-是否可以为Ruby创建Crystal绑定(bind)?,我们在StackOverflow上找到一个类

  8. ruby - Haskell 通过 FFI 与 Ruby 绑定(bind)? - 2

    由于ruby和Haskell都支持FFI,是否可以通过FFI从ruby​​调用Haskell代码?Ruby中有任何Haskell绑定(bind)吗? 最佳答案 我对这个讨论有点迟了,但我目前正在编写Ruby和Haskell之间的桥梁。它位于http://github.com/mwotton/Hubris-它是在C级别工作的绑定(bind)。不过,仍处于非常早期的开发阶段。 关于ruby-Haskell通过FFI与Ruby绑定(bind)?,我们在StackOverflow上找到一个类似的

  9. ruby - Ruby 的方法解除绑定(bind)机制有什么意义? - 2

    Method#unbind返回对该方法的UnboundMethod引用,稍后可以使用UnboundMethod#bind将其绑定(bind)到另一个对象.classFooattr_reader:bazdefinitialize(baz)@baz=bazendendclassBardefinitialize(baz)@baz=bazendendf=Foo.new(:test1)g=Foo.new(:test2)h=Bar.new(:test3)f.method(:baz).unbind.bind(g).call#=>:test2f.method(:baz).unbind.bind(h).

  10. ruby - Ruby 中绑定(bind)/解除绑定(bind)方法的目的是什么? - 2

    Method#unbind和UnboundMethod#bind的目的是什么?据我所知,方法是像procs和lambdas这样的可调用对象,除了方法绑定(bind)到它们的接收者的范围之外:classSomeClassdefa_method;puts"fromSomeClass";endends=SomeClass.news.a_method#=>"fromSomeClass"如果我在SomeClass的上下文中或者我有一个SomeClass的对象,我可以调用a_method。我可以通过将方法提取为Method对象使其成为可调用对象,但在此示例中它仍然绑定(bind)到SomeClas

随机推荐