zoukankan      html  css  js  c++  java
  • Difference between x:Reference and x:Name

    {x:Reference ...} -> returns just a reference of an object it doesn't create that "bridge" between two properties like binding would do. Behind all that a service is being used that searches for the given name in a specific scope which is usually the window itself.

    {Binding ElementName="..." } -> first of all it creates that binding object then it searches for the object name but not by using the same technique under the hood as x:Reference. The search algorithm moves up and/or down in VisualTree to find the desired element. Therefore a functional VisualTree is always needed. As example when used inside a Non-UiElement, it won't work. In the end the Binding stays and does its daily bread.

    This won't work:

    <StackPanel>
     <Button x:name="bttn1" Visibility="Hidden">Click me</Button>
     <DataGrid>
      <DataGrid.Columns>
        <DataGridTextColumn Visibility="{Binding ElementName=bttn1, Path=DataContext.Visibility}"/>
     ....

    This works:

    <StackPanel>
     <Button x:name="bttn1" Visibility="Hidden">Click me</Button>
     <DataGrid>
      <DataGrid.Columns>
        <DataGridTextColumn Visibility="{Binding Source={x:Reference bttn1} Path=DataContext.Visibility}"/>
     ....

    Sort of like that :)

    作者:qidong
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    this 指向 及 调用方式
    页面布局上 左 右底部
    Thread Safety线程安全
    sql查删更
    LinuxMysql命令操作数据库
    excel链接sharepoint 用于 Excel 的 Microsoft Power Query
    W7无法更新
    发票点数计算
    sharepoint OWA问题解决
    zend studio 做前端推荐安装的插件
  • 原文地址:https://www.cnblogs.com/qidong/p/5190724.html
Copyright © 2011-2022 走看看