大佬教程收集整理的这篇文章主要介绍了silverlight-2.0 – 自定义控件中的TemplateBindings,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我在generic.xaml中的ControlTemplate看起来像
<resourceDictionary xmlns="http://scheR_91_11845@as.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://scheR_91_11845@as.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?有没有办法围绕这个,因为我可以看到它发生了很多?
尽管如此,TemplateBinding不支持转换器.您必须将TemplateBinding添加到DataContext,然后在Text属性中使用常规Binding以及转换器.
所以TextBlock标记就变成了
<TextBlock Width="50" Height="30" DataContext="{TemplateBinding value}" Text="{Binding Converter={Staticresource numberTypeToStringConverter}}" />
我的定制转换器:
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,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 }); } }
这似乎是一种解决方法,我有兴趣听听它是否有任何不利影响.此外,如果有人正在阅读这个,我的转换器有任何问题,请告诉我.
干杯
以上是大佬教程为你收集整理的silverlight-2.0 – 自定义控件中的TemplateBindings全部内容,希望文章能够帮你解决silverlight-2.0 – 自定义控件中的TemplateBindings所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。