WPF:使用 TemplateBinding 将整数绑定到 TextBlock

发布于 2024-09-05 20:29:13 字数 2228 浏览 9 评论 0原文

我在 WPF 中有一个自定义控件。在此我有一个 int 类型的 DependencyProperty。在自定义控件的模板中,我有一个 TextBlock,我想显示 TextBlock 中的整数值。但我无法让它发挥作用。

我正在使用TemplateBinding。如果我使用相同的代码,但将 DependencyProperty 的类型更改为 string 它工作正常。但我真的希望它是一个整数,以便我的应用程序的其余部分能够正常工作。

我该怎么做?

我已经编写了显示问题的简化代码。首先是自定义控件:

public class MyCustomControl : Control
{
    static MyCustomControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new FrameworkPropertyMetadata(typeof(MyCustomControl)));

        MyIntegerProperty = DependencyProperty.Register("MyInteger", typeof(int), typeof(MyCustomControl), new FrameworkPropertyMetadata(0));
    }


    public int MyInteger
    {
        get
        {
            return (int)GetValue(MyCustomControl.MyIntegerProperty);
        }
        set
        {
            SetValue(MyCustomControl.MyIntegerProperty, value);
        }
    }
    public static readonly DependencyProperty MyIntegerProperty;
}

这是我的默认模板:

<Style TargetType="{x:Type local:MyCustomControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyCustomControl}">
                <Border BorderThickness="1" CornerRadius="4" BorderBrush="Black" Background="Azure">
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{TemplateBinding MyInteger}" HorizontalAlignment="Center" />
                    </StackPanel>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

和用法:

<Window x:Class="CustomControlBinding.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CustomControlBinding"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <local:MyCustomControl Width="100" Height="100" MyInteger="456" />
</Grid>

我做错了什么?

谢谢//大卫

I have a custom control in WPF. In this I have a DependencyProperty of the type int. In the template for the custom control I have a TextBlock, I and would like to show the value of the integer in the TextBlock. But I can't get it to work.

I'm using TemplateBinding. If I use the same code but change the type of the DependencyProperty to string it works fine. But I really want it to be an integer for the rest of my application to work.

How can I do this?

I've written simplified code that shows the problem. First the custom control:

public class MyCustomControl : Control
{
    static MyCustomControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new FrameworkPropertyMetadata(typeof(MyCustomControl)));

        MyIntegerProperty = DependencyProperty.Register("MyInteger", typeof(int), typeof(MyCustomControl), new FrameworkPropertyMetadata(0));
    }


    public int MyInteger
    {
        get
        {
            return (int)GetValue(MyCustomControl.MyIntegerProperty);
        }
        set
        {
            SetValue(MyCustomControl.MyIntegerProperty, value);
        }
    }
    public static readonly DependencyProperty MyIntegerProperty;
}

And this is my default template:

<Style TargetType="{x:Type local:MyCustomControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyCustomControl}">
                <Border BorderThickness="1" CornerRadius="4" BorderBrush="Black" Background="Azure">
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{TemplateBinding MyInteger}" HorizontalAlignment="Center" />
                    </StackPanel>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

And usage:

<Window x:Class="CustomControlBinding.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CustomControlBinding"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <local:MyCustomControl Width="100" Height="100" MyInteger="456" />
</Grid>

What am I doing wrong?

Thanks // David

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

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

发布评论

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

评论(1

﹂绝世的画 2024-09-12 20:29:13

尝试使用常规 BindingRelativeSourceTemplatedParent

<TextBlock Text="{Binding MyInteger, RelativeSource={RelativeSource TemplatedParent}}" HorizontalAlignment="Center" />

根据 此线程,这是 TemplateBinding 的限制:

TemplateBinding是一个轻量级的
“绑定”,它不支持某些
传统 Binding 的特点,例如
作为自动类型转换使用
相关的已知类型转换器
与目标属性

Try using a regular Binding with a RelativeSource of TemplatedParent:

<TextBlock Text="{Binding MyInteger, RelativeSource={RelativeSource TemplatedParent}}" HorizontalAlignment="Center" />

According to this thread, this is a limitation of TemplateBinding:

TemplateBinding is a lightweight
"binding", it doesn't support some
features of traditional Binding, such
as automatically type conversion using
the known type converters associated
with the target property

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