[英]ContentControl not showing Content
I have a custom ContentControl
which has a fixed XAML layout like a UserControl
(Rather than the usual applied generic template).我有一个自定义的ContentControl
,它具有固定的 XAML 布局,如UserControl
(而不是通常应用的通用模板)。
Previously this layout had no extra markup, so it was literally:以前这个布局没有额外的标记,所以它的字面意思是:
<ContentControl x:Class="MyControls.CustomViewControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
</ContentControl>
This worked fine.这工作得很好。
I now want to put a border around the content, so I have changed the XAML to:我现在想在内容周围放置一个边框,因此我已将 XAML 更改为:
<ContentControl x:Class="MyControls.CustomViewControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<ContentControl.Template>
<ControlTemplate>
<Border BorderThickness="5" BorderBrush="LightGreen">
<ContentPresenter />
</Border>
</ControlTemplate>
</ContentControl.Template>
</ContentControl>
This shows the border, but no content.这显示了边框,但没有内容。
I tried supplying an explicit binding for ContentPresenter:我尝试为 ContentPresenter 提供显式绑定:
<ContentPresenter Content="{Binding Path=Content, RelativeSource={RelativeSource Self}}"/>
But this made no difference.但这并没有什么区别。
Setting an explicit Content
does work:设置显式Content
确实有效:
<ContentPresenter Content="TEST" />
Anyone know why the Content binding doesn't work?任何人都知道为什么内容绑定不起作用? I guess I could fall back to the usual generic template but it would be easier if I could just do it directly like a UserControl.我想我可以回到通常的通用模板,但如果我可以像 UserControl 一样直接执行它会更容易。
Add TargetType for Control Template为控制模板添加 TargetType
<ContentControl.Template>
<ControlTemplate TargetType="Button">
<Border BorderThickness="5" BorderBrush="LightGreen">
<ContentPresenter />
</Border>
</ControlTemplate>
</ContentControl.Template>
Use TemplateBinding
instead of Binding
inside a ControlTemplate
:在ControlTemplate
使用TemplateBinding
而不是Binding
:
<ContentControl.Template>
<ControlTemplate TargetType="ContentControl">
<Border BorderThickness="5" BorderBrush="LightGreen">
<ContentPresenter Content="{TemplateBinding Content}"/>
</Border>
</ControlTemplate>
</ContentControl.Template>
EDIT:编辑:
The important part of the shown code snippet is the TargetType
in the definition of the ControlTemplate
.所示代码片段的重要部分是ControlTemplate
定义中的TargetType
。 With the target type与目标类型
<ContentControl.Template>
<ControlTemplate TargetType="ContentControl">
<Border BorderThickness="5" BorderBrush="LightGreen">
<ContentPresenter/>
</Border>
</ControlTemplate>
</ContentControl.Template>
already works without any TemplateBinding
已经可以在没有任何TemplateBinding
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.