首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运行时WPF Binding.ValidationRules

运行时WPF Binding.ValidationRules
EN

Stack Overflow用户
提问于 2011-08-15 14:57:05
回答 1查看 1.8K关注 0票数 4

我正在为XAML中的各种控件编写验证规则。我希望在运行时将validationRules附加到控件,而不是在XAML中。我在考虑用转换器。但是任何能更好地实现这一目标的想法或想法。

样本代码:

代码语言:javascript
复制
 <TextBox Name="txtFirstName" >   <TextBox.Text>  <Binding Path="FirstName" ValidatesOnDataErrors="True" PropertyChanged" >
          <Binding.ValidationRules>
                <Binding Converter="{StaticResource validationConverter}"/>
           </Binding.ValidationRules>             
    </Binding>                                               
代码语言:javascript
复制
public class ValidationConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        Binding b = new Binding();
        b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        b.ValidatesOnDataErrors = true;
        b.NotifyOnValidationError = true;
        b.ValidationRules.Add(new RangeValidationRule { ErrorMessage = "Error shown from Converter ", MinValue = 10, MaxValue = 50 });
        return b;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

谢谢,

EN

回答 1

Stack Overflow用户

发布于 2011-08-15 15:13:57

我使用过的一个方法是创建不同的样式,其中包含了我的自定义验证。然后,只需在运行时分配包含所需验证的适当样式即可。

编辑

下面是创建一个名为NumericTextBox的样式的基本示例:

代码语言:javascript
复制
<Style x:Key="NumericTextBox">
    <Setter Property="TextBox.VerticalAlignment"
            Value="Stretch"/>
    <Setter Property="TextBox.VerticalContentAlignment"
            Value="Center"/>
    <Setter Property="TextBox.Height"
            Value="24"/>
    <Setter Property="TextBox.Margin"
            Value="0,2,0,2"/>
    <EventSetter Event="UIElement.PreviewTextInput"
                 Handler="tbx_DigitCheck"/>
    <EventSetter Event="UIElement.PreviewKeyDown"
                 Handler="tbx_OtherCharCheck"/>
    <EventSetter Event="UIElement.PreviewDragEnter"
                 Handler="tbx_PreviewDragEnter"/>

</Style>

下面的方法放在存储样式的资源字典的代码隐藏文件中。此方法确保只能在文本框中输入数字和有效的十进制分隔符。每个字符在实际显示在文本框中之前都会检查其有效性。

代码语言:javascript
复制
Public Sub tbx_DigitCheck(ByVal sender As Object, _
                       ByVal e As TextCompositionEventArgs)

  //Retireve the sender as a textbox.
  Dim tbx As TextBox = CType(sender, TextBox)
  Dim val As Short

  //Get the current decimal separator.
  Dim dSep As String = Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator

  //Check to make sure that a decimal separator character has not
  //already been entered in the textbox.  Only make this check
  //for areas of the text that are not selected.
  If e.Text = dSep And tbx.SelectedText.Contains(dSep) Then
     //Do Nothing.  Allow the character to be typed.
  ElseIf e.Text = dSep And tbx.Text.Contains(dSep) Then
     e.Handled = True
  End If

  //Only allow the decimal separator and numeric digits.
  If Not Int16.TryParse(e.Text, val) And Not e.Text = dSep Then
     e.Handled = True
  End If

End Sub
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7066474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档