WPF( 数据验证)
[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 !!!");
}
}
}
上一篇: 暴风影音怎么开启老板键?开启有什么功能?
下一篇: 百度音乐网页版可以自定义皮肤吗?