• 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
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    BloomFilter——读数学之美札记
    线性时间求最长回文子串
    python之装饰器详解
    多线程快速排序(思考)
    搬家
    webapp用户身份认证方案 JSON WEB TOKEN 实现
    Java多线程问题总结
    synchronized的4种用法
    线程池的工作原理及使用示例
    Http 请求 GET和POST的区别
  • 原文地址:https://www.cnblogs.com/qidong/p/5190724.html
Copyright © 2020-2023  润新知