当前位置: 移动技术网 > IT编程>开发语言>.net > WPF( 数据验证)

WPF( 数据验证)

2018年09月19日  | 移动技术网IT编程  | 我要评论

盖米阀门,为什么不抓捕陈方安生,江苏省本三分数线

[html] 
<window x:class="testofvalidationrules.mainwindow" 
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="https://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> 

<window x:class="testofvalidationrules.mainwindow"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://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>
[csharp] 
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 !!!"); 
        } 
    } 

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 !!!");
        }
    }
}


 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网