<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      享受代碼,享受人生

      SOA is an integration solution. SOA is message oriented first.
      The Key character of SOA is loosely coupled. SOA is enriched
      by creating composite apps.
        博客園  :: 首頁  :: 新隨筆  :: 聯(lián)系 :: 訂閱 訂閱  :: 管理

      How does ElementName Binding work? - Part 1 Logical Tree & NameScope

      Posted on 2010-05-28 17:31  idior  閱讀(3789)  評(píng)論(6)    收藏  舉報(bào)

      Most developers have used {Binding ElementName= myControl , Path = myProperty} in their WPF projects, however you may find it didn’t work for you in certain cases, especially when you were building a complex control in which lots of controls nested.

       

      To understand the ElementName binding, we must understand NameScope first, since ElementName binding will use NameScope.FindName to find control internally.

       

      I don’t want to talk about the concept of NameScope too much in this article; MSDN already gave a good description for it. It comes into play when you give a name to the control defined in the template, see following codes:

      <Window>

          <Window.Resources>

              <ControlTemplate x:Key="template"  TargetType="{x:Type Button}">

                  <Rectangle Name="foo"/>

              </ControlTemplate

          </Window.Resources>

                  <Button Template="{StaticResource template}"/>

                  <Button Template="{StaticResource template}"/>

      </Window>

       

      Without NameScope, there will be two buttons with the same name in the window, which will defiantly cause an error for us. How NameScope rescue?

       

      Every template has its own NameScope , an instance of TemplateNameScope type. I think this is known to most developer; however you may not heard of that every UserControl has it own NameScope.

       

      Now let’s see how NameScope.FindName works. Actually the idea is very simple; each element defined in the xaml with Name Property will be registered in a nameMap dictionary in its nearest NameScope. NameScope.FindName will simply get the element from the nameMap. You may wonder what’s the nearest NameScope? Here nearest means the nearest element which has a NameScope in the logic tree.

       

      One thing you should keep in mind is that not every element has a NameScope, the top level window has a NameScope, each UserControl has a NameScope, and each element which defined at the top level in a ControlTemplate has a NameScope.

       

       

      Notes

      TemplateNameScope is a little different from NameScope. Elements have names  which are defined in a template will not be registered into the nameMap. I don’t want to digger into that too much, you just need to know TemplateNameScope.FindName is not just relay on nameMap dictionary and it’s a little different from NameScope.FindName, but we can still use it to find the elements defined in the template by using its name.

       

       

      Now let’s go back to the element name binding. After studying the source codes of WPF, I find ElementName Binding use ElementObjectRef class for finding the element via its name. By looking into the source code, we can easily find how it works.

       

      1. Start from the element which applied the ElementName Binding, keep searching on the logic tree via its logic parent, until an element which has NameScope is found, let’s call it NameScopeElement. If no element owns a NameScope, search will stop.
      2. Call the NameScope.FindName method on the found NameScope.
      3. If the element is found, return it, otherwise try to get the template parent of NameScopeElement; if the template parent is null, it will stop search. or it goes back to step 1, search on the logic tree for element owns a NameScope.

       

       

      Now let’s see an example in which ElementName Binding doesn’t work properly.

       

      <Window x:Class="TestElementBindingInStyle.Window1"

          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

          xmlns:local="clr-namespace:TestElementBindingInStyle"

          Title="Window1" Height="300" Width="300" x:Name="root" >  

            <local:MyUC Grid.Row="0" x:Name="myuc" IsEnabled="false" >

              <local:MyUC.MyContent >

                <Button Content="{Binding ElementName=myuc, Path=IsEnabled}"  />

              </local:MyUC.MyContent>

            </local:MyUC>  

      </Window>

       

      <UserControl x:Class="TestElementBindingInStyle.MyUC"

          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

          xmlns:uc="clr-namespace:TestElementBindingInStyle"

          x:Name="root">

          <ContentPresenter Content="{Binding ElementName= root,Path= MyContent}" Grid.Row="0"/>

      </UserControl>

       

          public partial class MyUC : UserControl

          {

              public MyUC()   {

                  InitializeComponent();

              }

       

              /// <summary>

              /// MyContent Dependency Property

              /// </summary>

              public static readonly DependencyProperty MyContentProperty =

                  DependencyProperty.Register("MyContent", typeof(object), typeof(MyUC),

                      new FrameworkPropertyMetadata(null ,

                          new PropertyChangedCallback(OnMyContentChanged)));

       

              /// <summary>

              /// Gets or sets the MyContent property. This dependency property

              /// indicates ....

              /// </summary>

              public object MyContent

              {

                  get { return (object)GetValue(MyContentProperty); }

                  set { SetValue(MyContentProperty, value); }

              }

       

              /// <summary>

              /// Handles changes to the MyContent property.

              /// </summary>

              private static void OnMyContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

              {

                  ((MyUC)d).OnMyContentChanged(e);

              }

       

              /// <summary>

              /// Provides derived classes an opportunity to handle changes to the MyContent property.

              /// </summary>

              protected virtual void OnMyContentChanged(DependencyPropertyChangedEventArgs e)

              {

                   this.AddLogicalChild(e.NewValue);

              }

          }


       

      I have highlighted the binding which doesn’t work. Now let’s go though the finding path to see why it doesn’t work here.

      The search starts from myBtn, it will check whether it has a NameScope, apparently it doesn’t. Then we move to its logic parent which is myuc, since it’s a UserControl, it does have a NameScope; however we cannot find myuc in this Scope, because myuc is registered in the Window’s NameScope. Now it will try to get the TemplateParent of myuc, but the value is null, so the search stops. It cannot find Element whose name is myuc.

       

       

      Now my question is what‘s the correct approach for making this binding work? Use RelativeSource Binding. 

       

      <local:MyUc x:Name="myuc" IsEnabled="false">

                  <local:MyUc.MyContent >

                      <Button Content="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:MyUc}}}" />

                  </local:MyUc.MyContent>

      </local:MyUc> 

      順便做個(gè)廣告,道富信息科技和網(wǎng)新恒天都招聘.NET高級(jí)開發(fā)人員和架構(gòu)師。需要對(duì).NET和面向?qū)ο笤O(shè)計(jì)有比較深入的了解和較好的英文讀寫能力,如果有WPF 或Silverlight開發(fā)經(jīng)驗(yàn)更佳,工作地點(diǎn)杭州。簡歷請(qǐng)發(fā)至 nxu  [at] statestreet [dot] com。

       

       

       

      主站蜘蛛池模板: 久久国产乱子精品免费女| 久久精品国产99国产精品澳门| 大尺度国产一区二区视频 | 国产 麻豆 日韩 欧美 久久| 日本一卡2卡3卡四卡精品网站| 国产精品免费无遮挡无码永久视频 | 在线观看国产成人av片| 久久国产免费观看精品3| 亚洲日韩一区二区| 国产偷倩视频| 一本大道av人久久综合| 五月婷婷激情第四季| 91精品久久一区二区三区| 宜章县| 日本久久一区二区免高清| 极品美女扒开粉嫩小泬图片 | 国产精品久久久久久妇女| 色偷偷久久一区二区三区| 福利一区二区在线视频| 东京热一区二区三区在线| 精品久久人人做爽综合| 久久综合偷拍视频五月天| 夜夜添狠狠添高潮出水| 亚洲国产日韩精品久久| 久青草视频在线观看免费| 精品无套挺进少妇内谢| 看免费的无码区特aa毛片| 国产96在线 | 亚洲| 亚洲av综合久久成人网| 久久99国产精品尤物| 少妇人妻偷人精品免费| 真实单亲乱l仑对白视频| 无码人妻一区二区三区兔费| 亚洲 欧美 唯美 国产 伦 综合| 无线乱码一二三区免费看| 蜜臀91精品国产高清在线| 一区二区三区AV波多野结衣| 国产精品美女免费无遮挡| 项城市| 扒开双腿猛进入喷水高潮叫声| 成人福利国产午夜AV免费不卡在线|