WPF:使用 TemplateBinding 将整数绑定到 TextBlock
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用常规
Binding
和RelativeSource
为TemplatedParent
:根据 此线程,这是
TemplateBinding
的限制:Try using a regular
Binding
with aRelativeSource
ofTemplatedParent
:According to this thread, this is a limitation of
TemplateBinding
: