【问题标题】:Can't get DependencyProperty to work无法使 DependencyProperty 工作
【发布时间】:2011-10-29 19:42:40
【问题描述】:

我为 WPF 文本框编写了一个名为“IsValid”的小附加属性,如下所示:

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 中的任何方法都不会被调用。我错过了什么?

【问题讨论】:

  • 您能否验证该属性被分配给InputTypes.Double 而不是字符串"Double"
  • 我会再看看您对输入的处理。我不认为你的支票在做你认为的那样。 stackoverflow.com/questions/228532/…
  • @Rachel:是的,已经解决了这个问题(见答案),我可以非常肯定我确实得到了预期的价值。 WPF 负责解析。 @Brent:的确,你是绝对正确的。我也意识到我的解决方案是不够的。仅仅指定“double”或“integer”是不够的。我还需要指定允许的小数位数以及通常的最小值和最大值。所以,我决定继续构建几个MarkupExtension 类。生成的语法非常令人愉快。我对 WPF 很陌生,但到目前为止我不得不说它非常强大......

标签: c# wpf xaml attached-properties


【解决方案1】:

之前你可能得到一个错误,告诉你IsValid 需要从DependecyObject 派生,所以你补充说,你应该问自己为什么会这样。答案就在这里:

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

您尝试为IsValid 类型的对象注册一个普通属性,将其更改为RegisterAttached,它应该可以工作。 (我也会删除继承并将IsValid 设为静态类)

【讨论】:

  • 呃!我感觉自己像个混蛋。谢谢! :O)
【解决方案2】:

好的,所以问题的核心是微不足道的(请参阅接受的答案):我需要致电 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}" />

这一切似乎都如我所愿。感谢大家的意见

干杯

/乔纳斯

【讨论】:

    猜你喜欢
    • 2021-11-08
    • 1970-01-01
    • 2014-11-19
    • 2021-04-03
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多