TemplateBinding不适用于.NET框架对象

时间:2009-08-21 14:33:05

标签: wpf controltemplate templatebinding

我是使用ControlTemplate的新手。我正在写我的第一个控件,但我(在我看来)是一个非常奇怪的问题。

我使TemplateBinding工作的任何依赖项属性,但.NET框架对象中的任何属性,即Content的{​​{1}}属性或ContentControl属性<{1}}不会在运行时填充。

我确信我错过了什么......就是我不知道的......

代码示例如下:

此课程非常简单:

Items

模板是:

ItemsControl

基类public class Title : ContentControl { } 类是位于System.Windows.Controls.Control名称空间中的.NET类。

谢谢,

亚当

2 个答案:

答案 0 :(得分:1)

我相信如果你想覆盖放置Content的位置,你可以使用ContentPresenter来做到这一点。

<Style TargetType="{x:Type UI:Title}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type UI:Title}">
                <Label>
                    <ContentPresenter />
                </Label>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

注意我也从TextBlock更改为Label,因为我认为TextBlock.Text属性不会接受来自ContentControl.Content的所有内容。以下是我按照预期工作的示例:

<Window x:Class="ContentControlTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ContentControlTest"
        Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <Style TargetType="{x:Type local:Title}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:Title}">
                        <Button>
                            <ContentPresenter />
                        </Button>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <local:Title>
        <TextBlock Text="Happy Days!" />
    </local:Title>
</Window>

答案 1 :(得分:0)

您可能需要在对象上实现INotifyPropertyChanged接口,并在集合上实现INotifyCollectionChanged。

相关问题