silverlight   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了silverlight-2.0 – 自定义控件中的TemplateBindings大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我只是在使用Silverlight中的自定义控件,而对于我的生活,我无法使TemplateBindings工作.有人可以给这个缩小版本一次,看看我是否遗漏了什么. 所以我在generic.xaml中的ControlTemplate看起来像 <resourceDictionary xmlns="http://scheR_91_11845@as.microsoft.com/winfx/2006/xaml/presenta
我只是在使用Silverlight中的自定义控件,而对于我的生活,我无法使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?有没有办法围绕这个,因为我可以看到它发生了很多?

解决方法

经过一番挖掘后发现TextBlock实际上并没有在传入的任何内容调用ToString.要解决这个问题,你必须使用COnverter为你调用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,请注明来意。