TemplateBinding 不适用于 .NET Framework 对象

发布于 2024-08-02 11:04:56 字数 919 浏览 2 评论 0原文

我刚开始使用 ControlTemplate。我正在编写我的第一个控件,但我遇到了(在我看来)一个非常奇怪的问题。

我使 TemplateBinding 起作用的任何依赖项属性,但来自 .NET 框架对象的任何属性,即 ContentControlContent 属性或 < ItemsControl 的 code>Items 属性不会在运行时填充。

我确信我错过了一些东西...我不知道它是什么...

代码示例如下:

该类目前非常简单:

public class Title : ContentControl
{
}

模板是:

<Style TargetType="{x:Type UI:Title}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type UI:Title}">
                <TextBlock Text="{TemplateBinding Content}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

基本 ContentControl class 是位于 System.Windows.Controls.Control 命名空间中的 .NET 类。

谢谢,

亚当

I am new to using the ControlTemplate. I am writing my first control but I am having (what seems to me) a very strange issue.

Any dependency properties that I make TemplateBinding to work, but any properties from the .NET framework objects i.e. the Content property of a ContentControl or the Items property of an ItemsControl does not get populated at runtime.

I am sure I am missing something... Just what it is I dont know...

An example of the code is below:

The class is very simple at the moment:

public class Title : ContentControl
{
}

And the Template is:

<Style TargetType="{x:Type UI:Title}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type UI:Title}">
                <TextBlock Text="{TemplateBinding Content}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

The base ContentControl class is the .NET class located in the System.Windows.Controls.Control namespace.

Thanks,

Adam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何处潇湘 2024-08-09 11:04:56

我相信如果您想覆盖 内容放置在您可以使用内容演示者

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

I believe if you'd like to override where the Content is placed you can do that using a 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>

Note I've also changed from a TextBlock to a Label as I believe the TextBlock.Text property will not accept everything from ContentControl.Content. Here is an example I put together that works as intended:

<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>
池予 2024-08-09 11:04:56

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

You may need to implement the INotifyPropertyChanged interface on your objects and INotifyCollectionChanged on your collections.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文