ControlTemplate中的UserControl [英] UserControl within a ControlTemplate

查看:141
本文介绍了ControlTemplate中的UserControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Telerik Tile的ControlTemplate,并且我如下重写:

I have a ControlTemplate for a Telerik Tile and I am overriding like below:

<ControlTemplate TargetType="{x:Type ctrl:Tile}">
    <Border>  

        <local:UserControl>
            <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
        </local:UserControl>
    </Border>
</ControlTemplate>

我的用户控件如下:

    <DockPanel>
        <!-- some content -->

        <ContentPresenter/>

    </DockPanel>

ControlTemplate不显示UserControl的内容.

The ControlTemplate does not display the content of the UserControl.

如果我将控制模板更改为:

If I change my control template to:

<ControlTemplate TargetType="{x:Type ctrl:Tile}">
    <Border>  
        <StackPanel>
            <local:UserControl/>

            <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
       </StackPanel>
    </Border>
</ControlTemplate>

它将找到内容并将其适当放置.好像ControlTemplate嵌套在我的UserControl中之后就找不到内容.我有什么事可能做错了吗?

It will find the content and place it appropriately. It seems like the ControlTemplate cannot find the content once it's nested inside my UserControl. Is there anything I could be doing wrong?

请注意,这些ControlTemplate项目出现在ItemsPresenter中.

Note that these ControlTemplate Items are appearing in an ItemsPresenter.

推荐答案

您正在将UserControl视作基本的ContentControl(如Button),与实际的稍有不同.是.以Button为例,当您将一个子元素(即TextBlock)添加到Button元素中时,该元素实际上将该TextBlock设置为Button's Content属性.它呈现的方式是通过ButtonControlTemplate,其中包括将Content注入到其中的ContentPresenter.可视树的结局如下:

You're treating the UserControl as if it is a basic ContentControl (like a Button) which is a little different than what it actually is. Using Button as an example, when you add a child (i.e. a TextBlock) into a Button element that's actually setting that TextBlock as the Button's Content property. The way it gets rendered is through the Button's ControlTemplate, which includes a ContentPresenter to inject Content into. The Visual Tree ends up like this:

<Button>
  -start Template
  <Border>
    <ContentPresenter>
      -start Content
      <TextBlock>

到目前为止,基本上就是代码所遵循的模型.问题在于,您使用的是(仍是ContentControl派生的)UserControl,而不是通常使用XAML +代码隐藏模型定义的而不是使用ControlTemplate的模型,其中XAML定义了Content UserControl的值. (可以切换这些模型并以UserControl为模板或使用Button派生的类并在XAML +代码后面添加但不常见)

So far that's basically the model your code is following. The problem is that you're using a (still ContentControl derived) UserControl instead, which rather than using a ControlTemplate is most often defined with a XAML+code-behind model, where the XAML defines the Content of the UserControl. (It is possible to switch these models and template a UserControl or make a Button derived class with XAML+code-behind but not common)

如果您希望在XAML中正常定义UserControl的外观并且仍然能够注入其他内容,则可以添加另一个DependencyProperty来镜像Content属性的设置,并将内容设置为该内容.此方法与HeaderedContentControl衍生物(即Expander)一起使用,该衍生物基本上具有两个含量属性,ContentHeader.使用新属性如下所示:

If you want to both define the look of your UserControl in XAML as normal and still be able to inject other content you can add another DependencyProperty that mirrors the setup of the Content property and set your content to that. This approach is used with HeaderedContentControl derivatives (i.e. Expander) which essentially has 2 content properties, Content and Header. Using the new property would look like this:

<Border>  
    <local:UserControl>
      <local:UserControl.OtherContent>
          <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
      </local:UserControl.OtherContent>
    </local:UserControl>
</Border>

然后在UserControl的XAML中,您需要显式设置ContentPresenter绑定(您只能在ContentControls的模板中免费获得它们):

And then inside the UserControl's XAML you need to explicitly set up the ContentPresenter Bindings (you only get them for free inside templates of ContentControls):

<DockPanel>
    <!-- some content -->

    <ContentPresenter Content="{Binding Path=OtherContent, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"/>
</DockPanel>

如果需要ContentTemplateContentTemplateSelectorContentStringFormat,则还需要为其添加属性和绑定.

If you want a ContentTemplate, ContentTemplateSelector, or ContentStringFormat you'll also need to add properties and bindings for those.

这篇关于ControlTemplate中的UserControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆