Wpf - TemplateParts



You may give some template control names (magic names sometimes), but you might not be aware why is there the magic names, here is the deal.


Suppose that you were writing a text box, and suppose that you are the internal developer for the textbox, the requirement may sounds like this


You need to handle selection of text, including Commands like Ctrl+A

You need to handle Copy/Paste

You need to handle multiple languages and input methods

You need to position a caret between letters to show where the next keypress will be inserted


that is not enough, you are required to supports the following.

Users should be able to override everything about how the TextBox is displayed.


 Below is one of the template that I wrote for the custom text box.


     <TextBox>
      <TextBox.Template>
        <ControlTemplate TargetType="TextBox">
          <Border>
            <DockPanel>
              <Image Source="C:\WINDOWS\system32\DirectX\Dinput\mse.png" DockPanel.Dock="Right" />
              <ScrollViewer />
            </DockPanel>
          </Border>
        </ControlTemplate>
      </TextBox.Template>
    </TextBox>

Now, if you run it, you will find that you CANNOT input text, and there is no caret shown..


the reason is that there is no way to know where to render the caret, on the Image or on the ScrollViewer?


So , there is an assumption that the template will provide a control with specific name - PART_ContentHost


here is the revised code


     <TextBox>
      <TextBox.Template>
        <ControlTemplate TargetType="TextBox">
          <Border>
            <DockPanel>
              <Image Source="C:\WINDOWS\system32\DirectX\Dinput\mse.png" DockPanel.Dock="Right" />
              <ScrollViewer Name="PART_ContentHost/>
            </DockPanel>
          </Border>
        </ControlTemplate>
      </TextBox.Template>
    </TextBox>


why this is important, let's see the code


public override void OnApplyTemplate()
{
    base.OnApplyTemplate();

    anchor = Template.FindName("PART_ContentHost", this);
    // Store the anchor, so we can position the caret over the top of it later
}


But, how do you know the magic string PART_ContentHost? it is defined by the templateParent attribute.

[TemplatePart(Name="PART_ContentHost", Type=typeof(FrameworkElement))]
public abstract class TextBoxBase : A500TypeDeepHierarchyOfClasses, IButNoInterfaces

And how can I search for the Parts and their names ? Let's take the text box for example. TextBox Styles and Templates


Below shows the annotated code, just to give you a feel of it.


<TextBox>
      <TextBox.Template>
        <ControlTemplate TargetType="TextBox">
          <Border>
            <DockPanel>
              <Image Source="C:\WINDOWS\system32\DirectX\Dinput\mse.png" DockPanel.Dock="Right" />
              <!-- ![CDATA[ without an anchor, the text box cannot show the caret;
              An anchor point is a place where you know the text should go 
              
              <ScrollViewer />
              ]] -->
              
              <!-- ![CDATA[
              Follow up with the anchor point, you can do anything you like in the ControlTemplate, but you have to 
              remember to add a control with a specific name - in this case, PARTS_ContentHost
              ]] -->
              <!--
                <ScrollViewer Name="PART_ContentHost" />
              -->
              <ScrollViewer Name="PART_ContentHost" />
              
              
              <!-- ![CDATA[The reason is because 
              public override void OnApplyTemplate()
              {
                  base.OnApplyTemplate();

                  anchor = Template.FindName("PART_ContentHost", this);
                  // Store the anchor, so we can position the caret over the top of it later
              }
              
              
              however, how do you know the magic strings?
              
              It is applied with the TemplateParts attributes
              
              e..g
              
              [TemplatePart(Name="PART_ContentHost", Type=typeof(FrameworkElement))]
              public abstract class TextBoxBase : A500TypeDeepHierarchyOfClasses, IButNoInterfaces
              ..
              
              This example is based on the article by http://www.paulstovell.com/wpf-part-names
              
              ]]-->
            </DockPanel>
          </Border>
        </ControlTemplate>
      </TextBox.Template>
    </TextBox>



The article is based on the original post of Pall Stovell, the origianl article is here






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值