Hej,
Jeg har prøvet at køre det her kode(kode fra nettet), men det virker ikke..
findes der er nemmere måde at lave data validation på...???
- <Window x:Class="WPFWxample.FormValidation"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="FormValidation" Height="300" Width="300">
- <Window.Resources>
- <src:Customer x:Key="Emp"></src:Customer>
-
- <!--The Too tip for the textbox-->
- <Style x:Key="txterror" TargetType="{x:Type TextBox}">
- <Style.Triggers>
- <Trigger Property="Validation.HasError" Value="true">
- <Setter Property="ToolTip"
- Value="{Binding RelativeSource={x:Static RelativeSource.Self},
- Path=(Validation.Errors)[0].ErrorContent}"></Setter>
- <Setter Property="Background" Value="Red"></Setter>
- </Trigger>
- </Style.Triggers>
- </Style>
- </Window.Resources>
-
- <Grid>
- <Label Height="28" HorizontalAlignment="Left" Margin="12,27,0,0"
- Name="label1" VerticalAlignment="Top" Width="151">Emp. No</Label>
- <TextBox Height="23"
- Margin="262,27,96,0"
- Name="txteno"
- VerticalAlignment="Top" Style="{StaticResource txterror}">
- <TextBox.Text>
- <Binding Path="EmpNo" Source="{StaticResource Emp}"
- ValidatesOnDataErrors="True"
- UpdateSourceTrigger="PropertyChanged">
- <Binding.ValidationRules>
- <ExceptionValidationRule></ExceptionValidationRule>
- </Binding.ValidationRules>
- </Binding>
- </TextBox.Text>
- </TextBox>
- <Label Height="28" HorizontalAlignment="Left" Margin="12,72,0,0"
- Name="label2" VerticalAlignment="Top" Width="151">Emp. Name</Label>
- <Label HorizontalAlignment="Left" Margin="12,122,0,112" Name="label3"
- Width="151">Salary</Label>
- <TextBox Height="23" Margin="262,74,96,0" Name="txtename"
- VerticalAlignment="Top" Style="{StaticResource txterror}">
- <TextBox.Text>
- <Binding Path="EmpName" Source="{StaticResource Emp}"
- ValidatesOnDataErrors="True"
- UpdateSourceTrigger="PropertyChanged">
- <Binding.ValidationRules>
- <ExceptionValidationRule></ExceptionValidationRule>
- </Binding.ValidationRules>
- </Binding>
- </TextBox.Text>
- </TextBox>
- <TextBox Margin="262,127,96,112" Name="txtsal"
- Style="{StaticResource txterror}">
- <TextBox.Text>
- <Binding Path="Salary" Source="{StaticResource Emp}"
- ValidatesOnDataErrors="True"
- UpdateSourceTrigger="PropertyChanged">
- <Binding.ValidationRules>
- <ExceptionValidationRule></ExceptionValidationRule>
- </Binding.ValidationRules>
- </Binding>
- </TextBox.Text>
- </TextBox>
- <Button Height="23" Margin="12,0,96,55" Name="button1"
- VerticalAlignment="Bottom">Button</Button>
- </Grid>
- </Window>
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace WPFWxample
- {
-
- public class Customer : IDataErrorInfo
- {
- private int _EmpNo;
-
- public int EmpNo
- {
- get { return _EmpNo; }
- set { _EmpNo = value; }
- }
- private string _EmpName = "";
-
- public string EmpName
- {
- get { return _EmpName; }
- set { _EmpName = value; }
- }
- private int _Salary;
-
- public int Salary
- {
- get { return _Salary; }
- set { _Salary = value; }
- }
-
- #region IDataErrorInfo Members
-
- public string Error
- {
- get { return null; }
- }
-
- public string this[string fieldName]
- {
- get
- {
- string result = null;
-
- #region For Validating EmpNo
- if (fieldName == "EmpNo")
- {
- if (this._EmpNo < 0)
- {
- result = "Emp No should not be less than 0";
- }
- }
- #endregion
-
- #region For Validating EmpName
- if (fieldName == "EmpName")
- {
- if (this._EmpName == string.Empty)
- {
- result = "Employee Name should not be blank";
- }
- else
- {
- foreach (char c in this._EmpName)
- {
- if (c < 65 c > 122 (c >= 91 && c <= 96))
- {
- result = "Employee Name should " +
- "not Contain Special Characters";
- }
- }
- }
- }
- #endregion
-
- #region For Validating Salary
- if (fieldName == "Salary")
- {
- if (this._Salary < 0)
- {
- result = "Salary should not be less than Zero";
- }
- }
- #endregion
-
- return result;
- }
- }
-
- #endregion
- }
- }
Indlæg senest redigeret d. 30.11.2011 18:49 af Bruger #16802