Не удается заставить DependencyProperty работать - PullRequest
0 голосов
/ 18 августа 2011

Я написал небольшое прикрепленное свойство под названием «IsValid» для WPF TextBox, например:

public enum InputTypes
{
    Any,

    Integer,

    Double,

    Float
}

/// <summary>
/// This attached property can be used to validate input for <see cref="TextBox"/>. 
/// </summary>
public class IsValid : DependencyObject
{
    public static readonly DependencyProperty InputProperty = DependencyProperty.Register(
        "Input",
        typeof(InputTypes),
        typeof(IsValid),
        new UIPropertyMetadata(InputTypes.Any, onInput));

    public static InputTypes GetInput(DependencyObject d)
    {
        return (InputTypes)d.GetValue(InputProperty);
    }

    public static void SetInput(DependencyObject d, InputTypes value)
    {
        d.SetValue(InputProperty, value);
    }

    private static void onInput(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var textBox = (TextBox)d;
        var value = (InputTypes)e.NewValue;
        switch (value)
        {
            case InputTypes.Any:
                textBox.PreviewTextInput -= validateInput;
                textBox.PreviewKeyDown -= validateKeyDown;
                break;

            default:
                textBox.PreviewTextInput += validateInput;
                textBox.PreviewKeyDown += validateKeyDown;
                break;
        }
    }

    private static void validateInput(object sender, TextCompositionEventArgs e)
    {
        // enforce numeric input when configured ...
        var textBox = (TextBox) sender;
        var inputTypes = (InputTypes) textBox.GetValue(InputProperty);
        foreach (var c in e.Text)
        {
            switch (inputTypes)
            {
                case InputTypes.Integer:
                    if (!char.IsDigit(c))
                    {
                        e.Handled = true;
                        return;
                    }
                    break;

                case InputTypes.Double:
                case InputTypes.Float:
                    if (!char.IsNumber(c))
                    {
                        e.Handled = true;
                        return;
                    }
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
    }

    private static void validateKeyDown(object sender, KeyEventArgs e)
    {
        // block [SPACE] when numeric input is expected ...
        var textBox = (TextBox)sender;
        var inputTypes = (InputTypes)textBox.GetValue(InputProperty);
        if (inputTypes != InputTypes.Any && e.Key == Key.Space)
            e.Handled = true;
    }
}

Конец вот как я это использовал:

<Window x:Class="Spike.Wpf.Controls.TestApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:values="clr-namespace:Spike.Wpf.Controls.Input;assembly=Spike.Wpf.Controls"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <TextBox values:IsValid.Input="Double" />
</Grid>

После инициализации (DependencyProperty) ни один из методов в IsValid не вызывается. Чего мне не хватает?

Ответы [ 2 ]

2 голосов
/ 18 августа 2011

Ранее вы, вероятно, получили сообщение о том, что IsValid должно быть производным от DependecyObject, поэтому вы добавили, что вы должны были спросить себя, почему это так.Ответ прямо здесь:

public static readonly DependencyProperty InputProperty = DependencyProperty.Register(...

Вы пытаетесь зарегистрировать обычное свойство для объектов типа IsValid, измените его на RegisterAttached, и оно должно работать.(Я бы также удалил наследование и сделал IsValid статическим классом)

0 голосов
/ 20 августа 2011

Итак, суть проблемы была тривиальной (см. Принятый ответ): мне нужно было позвонить DependencyProperty.RegisterAttached(...) (в отличие от DependencyProperty.Register(...).

Просто хотел поделиться результатом. Я решил отказаться от использования простого enum для указания типа ввода и решил вместо этого использовать расширения разметки.

Реализация присоединенного свойства теперь выглядит следующим образом:

public static class IsValid
{
    public static readonly DependencyProperty InputProperty = DependencyProperty.RegisterAttached(
        "Input",
        typeof(IsValidInputExtension),
        typeof(IsValid),
        new UIPropertyMetadata(onInput));

    public static IsValidInputExtension GetInput(DependencyObject d)
    {
        return (IsValidInputExtension)d.GetValue(InputProperty);
    }

    public static void SetInput(DependencyObject d, IsValidInputExtension value)
    {
        d.SetValue(InputProperty, value);
    }

    private static void onInput(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var textBox = (TextBox)d;
        var value = (IsValidInputExtension)e.NewValue;
        if (value == null)
        {
            textBox.PreviewTextInput -= validateInput;
            textBox.PreviewKeyDown -= validateKeyDown;
            return;
        }
        textBox.PreviewTextInput += validateInput;
        textBox.PreviewKeyDown += validateKeyDown;
    }

    private static void validateInput(object sender, TextCompositionEventArgs e)
    {
        // dispatch validation to specified markup class ...
        var textBox = (TextBox) sender;
        var markup = (IsValidInputExtension)textBox.GetValue(InputProperty);
        markup.ValidateInput(sender, e);
    }

    private static void validateKeyDown(object sender, KeyEventArgs e)
    {
        // dispatch validation to specified markup class ...
        var textBox = (TextBox)sender;
        var markup = (IsValidInputExtension)textBox.GetValue(InputProperty);
        markup.ValidateKeyDown(sender, e);
    }
}

А вот часть классов расширения разметки:

public abstract class IsValidInputExtension : MarkupExtension
{
    internal abstract void ValidateInput(object sender, TextCompositionEventArgs e);
    internal abstract void ValidateKeyDown(object sender, KeyEventArgs e);
}

public class NumericExtension : IsValidInputExtension
{
    public double Minimum { get; set; }

    public double Maximum { get; set; }

    public uint Decimals { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }

    internal override void ValidateInput(object sender, TextCompositionEventArgs e)
    {
        var textBox = (TextBox) sender;
        if (isDecimalSeparator(e.Text) && Decimals == 0)
        {
            e.Handled = true;
            return;
        }

        // todo: honor Minimum and Maximum ...
    }

    private static bool isDecimalSeparator(string s)
    {
        return CultureInfo.CurrentUICulture.NumberFormat.CurrencyDecimalSeparator == s;
    }

    internal override void ValidateKeyDown(object sender, KeyEventArgs e)
    {
        // block [SPACE] when numeric input is expected ...
        e.Handled = e.Key == Key.Space;
    }
}

public class StringExtension : IsValidInputExtension
{
    public double MaximumLength { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }

    internal override void ValidateInput(object sender, TextCompositionEventArgs e)
    {
        // (nop)
    }

    internal override void ValidateKeyDown(object sender, KeyEventArgs e)
    {
        // todo: honor MaximumLength here
    }
}

Конечный результат в XAML довольно приятный и легко читаемый ...

<TextBox v:IsValid.Input="{v:Numeric Minimum=0, Maximum=99, Decimals=0}" />

Кажется, все работает, как я надеялся. Спасибо за все комментарии

Приветствия

/ Jonas

...