【问题标题】:ContentControl not showing ContentContentControl 不显示内容
【发布时间】:2012-09-24 05:28:54
【问题描述】:

我有一个自定义的 ContentControl,它有一个固定的 XAML 布局,例如 UserControl(而不是通常应用的通用模板)。

以前这个布局没有额外的标记,所以字面意思是:

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

这很好用。

我现在想在内容周围加一个边框,所以我将 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>

这显示了边框,但没有内容。

我尝试为 ContentPresenter 提供显式绑定:

<ContentPresenter Content="{Binding Path=Content, RelativeSource={RelativeSource Self}}"/>

但这并没有什么不同。

设置明确的Content 确实有效:

<ContentPresenter Content="TEST" />

有人知道为什么内容绑定不起作用吗?我想我可以回退到通常的通用模板,但如果我可以像 UserControl 一样直接执行它会更容易。

【问题讨论】:

    标签: c# wpf xaml binding contentcontrol


    【解决方案1】:

    ControlTemplate 中使用TemplateBinding 而不是Binding

    <ContentControl.Template>
        <ControlTemplate TargetType="ContentControl">
            <Border BorderThickness="5" BorderBrush="LightGreen">
                <ContentPresenter Content="{TemplateBinding Content}"/>
            </Border>
        </ControlTemplate>
    </ContentControl.Template>
    

    编辑:

    所示代码sn-p的重要部分是ControlTemplate定义中的TargetType。与目标类型

    <ContentControl.Template>
        <ControlTemplate TargetType="ContentControl">
            <Border BorderThickness="5" BorderBrush="LightGreen">
                <ContentPresenter/>
            </Border>
        </ControlTemplate>
    </ContentControl.Template>
    

    已经可以在没有任何TemplateBinding的情况下工作

    【讨论】:

    • 这不起作用,但它为我指明了正确的方向,因为我得到了一个错误。我需要设置模板的 TargetType。当我这样做时,我根本不需要任何绑定,因为它是隐式的。干杯。
    【解决方案2】:

    为控件模板添加 TargetType

    <ContentControl.Template>
        <ControlTemplate  TargetType="Button">
            <Border BorderThickness="5" BorderBrush="LightGreen">
                <ContentPresenter />
            </Border>
        </ControlTemplate>
    </ContentControl.Template>
    

    【讨论】:

    • 这是正确答案!就我而言,它是一种应用程序样式,而不是 ContentControl。还是一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    相关资源
    最近更新 更多