4

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 を呼び出さないのはなぜですか? 私はそれがたくさん起こっているのを見ることができるので、これを回避する方法はありますか?

4

2 に答える 2

11

少し掘り下げた後、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, 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 });
        }
    }

これはちょっとした回避策のように思えますが、何か悪影響があるかどうか知りたいです。また、誰かがこれを読んでいて、コンバーターに何か問題がある場合は、お知らせください。

乾杯

于 2009-04-17T10:10:33.550 に答える
0

問題を回避するためのさまざまなアプローチがあります。 この説明は Marek Latuskiewicz によって見つかりました

于 2010-02-14T20:45:59.490 に答える