Jam*_*Hay 4 silverlight-2.0 templatebinding
我只是在使用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>
Run Code Online (Sandbox Code Playgroud)
我的控件类看起来像:
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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我期待这个运行时TextBlock将显示数字20.任何想法为什么这不起作用?
作为一方,我没有一个单独的项目,其中包含对NumericStepperControl程序集的引用,并且当它运行时,控件似乎正确构建.
编辑...经过一些调查,我发现如果我将Value属性的类型更改为一个正常工作的字符串.为什么文本块不仅仅在传入任何内容时调用toString?有没有办法围绕这个,因为我可以看到它发生了很多?
Jam*_*Hay 11
经过一番挖掘后发现TextBlock实际上并没有在传入的任何内容上调用ToString.要解决这个问题,你必须使用Converter为你调用ToString.
尽管如此,TemplateBinding不支持转换器.您必须将TemplateBinding添加到DataContext,然后在Text属性中使用常规Binding以及转换器.
所以TextBlock标记就变成了
<TextBlock Width="50" Height="30" DataContext="{TemplateBinding Value}" Text="{Binding Converter={StaticResource NumberTypeToStringConverter}}" />
Run Code Online (Sandbox Code Playgroud)
我的定制转换器:
public class NumberTypeToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
{
throw new NullReferenceException();
}
return value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
MethodInfo methodInfo = targetType.GetMethod("Parse");
if (methodInfo == null)
{
throw new MissingMethodException("The targetType to convert back to a Number must implement a Parse method");
}
return methodInfo.Invoke(null, new object[] { value });
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎是一种解决方法,我有兴趣听听它是否有任何不利影响.此外,如果有人正在阅读这个,我的转换器有任何问题,请告诉我.
干杯
| 归档时间: |
|
| 查看次数: |
4161 次 |
| 最近记录: |