[英]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
会更容易一些,这里是您可能最终得到的代码。
<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
bound to the templated control properties, so a developer using it has more control over the looks. Border
绑定到模板化控件属性,因此使用它的开发人员可以更好地控制外观。 using System;
using Windows.UI.Xaml.Controls;
namespace SmallTests2018
{
public sealed class TemplatedControlWithContent : ContentControl
{
public TemplatedControlWithContent()
{
DefaultStyleKey = typeof(TemplatedControlWithContent);
}
}
}
ContentControl
. ContentControl
派生的。 <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>
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.