wpf - WPF Custom Control can not take direct content
问题描述
I can't put any direct content in my custom control, have a look:
<Style TargetType="local:MyCustomControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MyCustomControl">
<Grid>
<Viewport3D />
<!-- the viewport is working (proof provided) -->
<!-- both borders are needed -->
<Border>
<Border>
<ContentPresenter />
</Border>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
the class is derived from Control, in the static constructor DefaultStyleKeyProperty.OverrideMetadata
is set.
When I try to use MyCustomControl:
<local:MyCustomControl VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<TextBlock Margin="10,0,0,0" FontSize="16" Text="some test value" />
</local:MyCustomControl>
this error message is shown:
Cannot add content to object of type MyCustomControl MyNamespace.MyCustomControl
What could be the problem? Is somthing wrong with the Contentpresenter?
解决方案
我认为您应该将您的内容绑定到您的演示者
<ContentPresenter Content="{TemplateBinding Content}"/>
推荐阅读
- azure - Azure 管道 - 在功能分支上触发
- python-3.x - 如何紧固Knn算法进行实时人脸识别
- database - dolphindb中如何配置每个数据节点的参数?
- javascript - 如果对象属性值符合某些条件,如何在反应中呈现数据?
- pdf - 如何使用 PHP 处理 PDF/A1-a 格式的 PDF
- sql-server - 在 SQL Server 上读取 XML:选择自定义属性
- grafana - 流入 grafana 的 k6 指标
- css - 使一个部分在 Material-ui 中的其他两个部分之间占据完整的空间
- apache-spark - 通过databricks环境将2200万+条记录加载到neo4j数据库中
- pdf - NetSuite Advanced PDF 条件显示文本