限制WPF中的输入

时间:2013-07-11 14:23:37

标签: c# .net wpf xaml

我正在创建一个自定义控件,在XAML中调用时可以设置为仅允许某些类型的输入:

<lib:CustomControl RestrictTo="UnsignedIntegersOnly" ... ></CustomControl>

UnsignedIntegersOnly是Enum的一部分,其中包含一组允许的限制。

如果用户输入了不允许的内容,控件将抛出验证错误,不允许他继续下一个表单/ page / etc.

我实现这一点的愿景是,在构成此控件的基础TextBox中,将其文本字段绑定到验证规则,该规则将作为输入传递给CustomControl XAML声明中指定的RestrictTo值。然后在该ValidationRule类中,处理RestrictTo特定验证并返回验证是否成功。

这是我不太确定如何继续的地方。是否有可能以这种看似动态的方式将参数传递给ValidationRule?我正在设置我的控件的属性RestrictTo,然后将其传递给它的验证。

如果有可能,会怎么做?我应该使用哪种绑定或资源链接?

2 个答案:

答案 0 :(得分:1)

您可能对使用MaskedTextBox控件感兴趣,它会限制用户可以在TextBox中输入的内容。

由于微软对WPF没有官方控制权,我建议以下来自Xceed:

MaskedTextBox(可以免费使用: - )

这里有掩码的语法:

http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask.aspx

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <xctk:MaskedTextBox Mask="0000"></xctk:MaskedTextBox>
    </Grid>
</Window>

如果你有Visual Studio 2012,你可以通过NuGet轻松获得这个包:

(右键点击你的项目)

enter image description here

注意:在我对上一个问题的回答中,我在我发布的链接中广泛使用了验证规则,但我会说,如果有时候你可以通过精心设计的组件/控件来避免它那么这样做是明智的。正如@Barn在你之前提出的问题上所指出的那样,一般的验证规则可能很难做,而且有些可疑,因为你必须处理其中的所有类型,IMO它有点反直觉,因为验证器和转换器一般具体反对多面手;你可能会浪费更多时间而不是它的价值,它可能会比你想象的更少重复使用。 (来源:我的经历)

答案 1 :(得分:0)

下面的代码可以帮助您入门。它是用户控件而不是自定义控件。我建议您首先将代码作为用户控件使用,然后将其转换为自定义控件。将有效属性绑定到viewmodel中控制用户工作流的某个属性。

XAML:

<UserControl x:Class="WpfApplication.ValidatingControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" >
    <StackPanel Orientation="Horizontal">
        <TextBox Name="_textBox" TextChanged="OnTextChanged" Background="LightGray" Width="200"/>
        <TextBlock Name="_messageText" Foreground="Red" />
    </StackPanel>
</UserControl>

代码背后:

using System.Windows;
using System.Windows.Controls;

namespace WpfApplication
{
    public partial class ValidatingControl : UserControl
    {
        public ValidatingControl()
        {
            InitializeComponent();
        }

        public enum Restrictions
        {
            UnsignedIntegersOnly,
            SmallIntegersOnly        
        }

        public static readonly DependencyProperty RestrictToProperty =
            DependencyProperty.Register("RestrictTo", typeof(Restrictions), typeof(ValidatingControl), new PropertyMetadata(Restrictions.UnsignedIntegersOnly));

        public Restrictions RestrictTo
        {
            get { return (Restrictions)GetValue(RestrictToProperty); }
            set { SetValue(RestrictToProperty, value); }
        }

        public bool Valid
        {
            get { return (bool)GetValue(ValidProperty); }
            set { SetValue(ValidProperty, value); }
        }

        public static readonly DependencyProperty ValidProperty =
            DependencyProperty.Register("Valid", typeof(bool), typeof(ValidatingControl), new UIPropertyMetadata(false));

        private void OnTextChanged(object sender, TextChangedEventArgs e)
        {
            ValidateText(RestrictTo, _textBox.Text);
        }

        private void ValidateText(Restrictions restrictTo, string text)
        {
            // validate text, update _messageText, update Valid 
        }
    }
}