• WPF( 数据验证)


    <Window x:Class="TestOfValidationRules.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <StackPanel >
            <TextBox x:Name="textBox1" Margin="5" />
            <Slider x:Name="slider1" Minimum="-10" Maximum="110" Margin="5" />
            
        </StackPanel>
    </Window>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace TestOfValidationRules
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                Binding binding = new Binding("Value")
                {
                     Source = this.slider1
                };
    
                binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                
                RangeValidateRule rvr = new RangeValidateRule();
                rvr.ValidatesOnTargetUpdated = true;
                binding.ValidationRules.Add(rvr);
    
                binding.NotifyOnValidationError = true;
                this.textBox1.SetBinding(TextBox.TextProperty, binding);
                this.textBox1.AddHandler(Validation.ErrorEvent,new RoutedEventHandler(this.ValidationError));
            }
    
            void ValidationError(object sender, RoutedEventArgs e)
            {
                if (Validation.GetErrors(this.textBox1).Count > 0)
                {
                    this.textBox1.ToolTip = Validation.GetErrors(this.textBox1)[0].ErrorContent.ToString();
                }
            }
    
            
        }
    
        public class RangeValidateRule:ValidationRule
        {
            public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
            {
                double d = 0;
                if (double.TryParse(value.ToString(),out d))
                {
                    if(d >= 0 && d <= 100)
                    {
                        return new ValidationResult(true,null);
                    }
                }
    
                return new ValidationResult(false,"Validation Failed !!!");
            }
        }
    }
    


  • 相关阅读:
    POJ2425 A Chess Game[博弈论 SG函数]
    POJ1740A New Stone Game[组合游戏]
    Vijos P1196吃糖果游戏[组合游戏]
    CF724D. Dense Subsequence[贪心 字典序!]
    CF724B. Batch Sort[枚举]
    CF731C. Socks[DFS 贪心]
    CF733D Kostya the Sculptor[贪心 排序]
    CF733C Epidemic in Monstropolis[模拟 构造 贪心]
    洛谷P1991无线通讯网[kruskal | 二分答案 并查集]
    NOIP2015斗地主[DFS 贪心]
  • 原文地址:https://www.cnblogs.com/wjchang/p/3671537.html
Copyright © 2020-2023  润新知