我只是在silverlight 中处理自定义控件,而在我的一生中,我无法让TemplateBindings 工作。有人可以给这个简化版本一次,看看我是否遗漏了什么。
所以我在 generic.xaml 中的 ControlTemplate 看起来像
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:NumericStepperControl;assembly=NumericStepperControl">
<Style TargetType="local:NumericStepper">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:NumericStepper">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Border Grid.Column="0" BorderBrush="Black" BorderThickness="2" Width="50" Height="30">
<TextBlock Width="50" Height="30" Text="{TemplateBinding Value}" />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
我的控制类看起来像:
namespace NumericStepperControl
{
public class NumericStepper : Control
{
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(int), typeof(NumericStepper), new PropertyMetadata(20));
public NumericStepper()
: base()
{
DefaultStyleKey = typeof( NumericStepper );
}
public int Value
{
get
{
return (int)GetValue(ValueProperty);
}
set
{
SetValue(ValueProperty, value);
}
}
}
}
我期待当它运行时 TextBlock 将显示数字 20。关于为什么这不起作用的任何想法?
另一方面,我有一个单独的项目,其中包含对 NumericStepperControl 程序集的引用,并且当它运行时,控件似乎构建正确。
编辑...经过更多调查后,我发现如果我将 Value 属性的类型更改为可以正常工作的字符串。为什么文本块不只是在传递给它的任何内容上调用 toString ?有没有办法解决这个问题,因为我可以看到它发生了很多?