首页 文章

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

提问于
浏览
1

我创建了一个模板控件 . 我所做的所有默认样式都是添加内容演示者 . 我还引用了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);
    }

}

我没有对控件的.cs代码进行任何更改 . 我尝试设置内容,但被告知控件不允许直接内容 .

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

    </StackPanel>

我应该怎么做才能使用内容演示者?

如果我尝试使用用户控件,相同的方法可以正常工作 .

2 回答

  • 2

    虽然答案是由@DK发布的 . 很完美,我试图使用Control而不是ContentControl . 原因是因为我只是在努力熟悉UWP .

    他的回答非常有助于我解决继承自Control的控件的问题 .

    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>
    
  • 1

    要在自定义模板化控件中处理XAML内容,您必须从ContentControl派生控件或保持继承自Control,实现自定义ContentProperty并将ContentPresenter绑定到它 .

    使用 ContentControl 会更容易一些,这里's the code you'可能最终会结束 .

    Themes / 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>
    
    • 我更喜欢将 Border 绑定到模板化控件属性,因此使用它的开发人员可以更好地控制外观 .

    • 这里的椭圆是一些额外自定义内容的示例 .

    TemplatedControlWithContent.cs

    using System;
    using Windows.UI.Xaml.Controls;
    
    namespace SmallTests2018
    {
        public sealed class TemplatedControlWithContent : ContentControl
        {
            public TemplatedControlWithContent()
            {
                DefaultStyleKey = typeof(TemplatedControlWithContent);
            }
        }
    }
    
    • 请注意,此处唯一的变化是控件来自 ContentControl .

    测试页面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>
    

    它在页面XAML设计器中的外观

相关问题