我只是自定义控件瞎在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上无论是传递给它?是否有办法解决这,我可以看到它发生了很多?

有帮助吗?

解决方案

挖掘一下它的关闭以后出了TextBlock的其实并不调用toString无论是在传递。要解决这一点,你必须使用一个转换器来调用一个ToString你。

下面是擦虽然TemplateBinding不支持转换器。您必须添加TemplateBinding到DataContext,然后与转换器一起正常使用绑定的Text属性。

所以将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, 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 });
        }
    }

这似乎有点像一个工作的周围,我很想听听如果有任何不利的影响。此外,如果任何人在读这并没有什么错我的转换器,请让我知道。

干杯

其他提示

有不同的方法来得到角落找寻问题。 找到此由马立克Latuskiewicz 描述。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top