简体   繁体   English

模板控制中的ContentPresenter无法正常工作

[英]ContentPresenter in Templated Control not working

I have created a templated control. 我创建了一个模板控件。 All I did to the default style was add a content presenter. 我所做的所有默认样式都是添加内容演示者。 I also referenced the Generic.xaml in the App.xaml file. 我还引用了App.xaml文件中的Generic.xaml。

<Style TargetType="local2:TestingControl" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local2:TestingControl">
                <Border
                    Height="200px"
                    Background="Green">

                    <ContentPresenter />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


public sealed class TestingControl : Control
{
    public TestingControl()
    {
        this.DefaultStyleKey = typeof(TestingControl);
    }

}

I haven't made any changes to the .cs code of the control. 我没有对控件的.cs代码进行任何更改。 I tried setting the content, but was told that the control does not allow direct content. 我尝试设置内容,但被告知控件不允许直接内容。

 <StackPanel>
        <local1:TestingControl >
            Testing
        </local1:TestingControl>

    </StackPanel>

在此输入图像描述 What should i be doing to make use of the content presenter? 我应该怎么做才能使用内容演示者?

The same approach works perfectly if i try to use a user control. 如果我尝试使用用户控件,相同的方法可以正常工作。

To handle XAML content in a custom Templated Control you have to either derive your control from the ContentControl or stay inherited from a Control , implement a custom ContentProperty and bind a ContentPresenter to it. 为了处理自定义模板化控件,你必须要么从派生您控制XAML内容ContentControl或从保持遗传Control ,实现自定义ContentProperty和绑定ContentPresenter它。

Going with the ContentControl is a bit easier, and here's the code you'd probably end up with. 使用ContentControl会更容易一些,这里是您可能最终得到的代码。

Style definition in Themes/Generic.xaml 主题/ Generic.xaml中的样式定义

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:local="using:SmallTests2018">

    <Style TargetType="local:TemplatedControlWithContent" >
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:TemplatedControlWithContent">
                    <Border
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                        <Viewbox>
                            <Grid>
                                <Viewbox HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                    <Ellipse Width="10" Height="10" Fill="#80808080" />
                                </Viewbox>
                                <ContentPresenter />
                            </Grid>
                        </Viewbox>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
  • I prefer to keep Border bound to the templated control properties, so a developer using it has more control over the looks. 我更喜欢将Border绑定到模板化控件属性,因此使用它的开发人员可以更好地控制外观。
  • An ellipse here is an example of some extra custom content. 这里的椭圆是一些额外自定义内容的示例。

TemplatedControlWithContent.cs TemplatedControlWithContent.cs

using System;
using Windows.UI.Xaml.Controls;

namespace SmallTests2018
{
    public sealed class TemplatedControlWithContent : ContentControl
    {
        public TemplatedControlWithContent()
        {
            DefaultStyleKey = typeof(TemplatedControlWithContent);
        }
    }
}
  • note that the only change here is that control is derived from ContentControl . 请注意,此处唯一的变化是控件是从ContentControl派生的。

A test page TemplatedControlWithContentPage.xaml 测试页面TemplatedControlWithContentPage.xaml

<Page
    x:Class="SmallTests2018.TemplatedControlWithContentPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SmallTests2018"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <local:TemplatedControlWithContent>
        <TextBlock>Hello World!</TextBlock>
    </local:TemplatedControlWithContent>
</Page>

How it looks in the page XAML designer 它在XAML设计器页面中的外观

在此输入图像描述

While the answer posted by @DK. 虽然答案是由@DK发布的。 was perfect, I was trying to use a Control instead of a ContentControl. 很完美,我试图使用Control而不是ContentControl。 The reason why is because i'm simply messing around trying to familiarize myself with UWP. 原因是因为我只是在努力熟悉UWP。

His answer was extremely helpful in getting me to solve the issue for a control inheriting from Control. 他的回答非常有助于我解决继承自Control的控件的问题。

TestingControl.cs TestingControl.cs

[ContentProperty(Name = "Content")]
public sealed class TestingControl : Control
{
    public TestingControl()
    {
        this.DefaultStyleKey = typeof(TestingControl);
    }

    public object Content
    {
        get { return (string)GetValue(ContentProperty); }
        set { SetValue(ContentProperty, value); }
    }

    public static readonly DependencyProperty ContentProperty =
        DependencyProperty.Register("Content", typeof(string), typeof(TestingControl), new PropertyMetadata(string.Empty));

}

Style 样式

<Style TargetType="local2:TestingControl" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local2:TestingControl">
                <Border
                    Height="200px"
                    Background="Green"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">

                    <ContentPresenter Content="{TemplateBinding Content}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Usage 用法

    <StackPanel>
        <local1:TestingControl >                
                Testing                
        </local1:TestingControl>
    </StackPanel>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM