Bladeren bron

Валидация данных

Danila Alekseev 2 jaren geleden
bovenliggende
commit
88d56d9b40
4 gewijzigde bestanden met toevoegingen van 64 en 1 verwijderingen
  1. 12 0
      LR1/Dictionary1.xaml
  2. 1 0
      LR1/LR1.csproj
  3. 33 0
      LR1/ValidationRules/EmailRule.cs
  4. 18 1
      LR1/Workers.xaml

+ 12 - 0
LR1/Dictionary1.xaml

@@ -53,4 +53,16 @@
     <DataTemplate x:Key="EditingDataTemplate">
     <DataTemplate x:Key="EditingDataTemplate">
         <DatePicker SelectedDate="{Binding BirstDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
         <DatePicker SelectedDate="{Binding BirstDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
     </DataTemplate>
     </DataTemplate>
+
+    <Style x:Key="errorStyle" TargetType="{x:Type TextBox}">
+        <Setter Property="Padding" Value="-2"/>
+        <Style.Triggers>
+            <Trigger Property="Validation.HasError" Value="True">
+                <Setter Property="Background" Value="Red"/>
+                <Setter Property="BorderThickness" Value="1" />
+                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
+            </Trigger>
+        </Style.Triggers>
+    </Style>
+        
 </ResourceDictionary>
 </ResourceDictionary>

+ 1 - 0
LR1/LR1.csproj

@@ -103,6 +103,7 @@
       <DependentUpon>MainPage.xaml</DependentUpon>
       <DependentUpon>MainPage.xaml</DependentUpon>
     </Compile>
     </Compile>
     <Compile Include="Model\ListTitle.cs" />
     <Compile Include="Model\ListTitle.cs" />
+    <Compile Include="ValidationRules\EmailRule.cs" />
     <Compile Include="Workers.xaml.cs">
     <Compile Include="Workers.xaml.cs">
       <DependentUpon>Workers.xaml</DependentUpon>
       <DependentUpon>Workers.xaml</DependentUpon>
     </Compile>
     </Compile>

+ 33 - 0
LR1/ValidationRules/EmailRule.cs

@@ -0,0 +1,33 @@
+using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows.Controls;
+
+namespace LR1.ValidationRules
+{
+    internal class EmailRule: ValidationRule
+    {
+        //public override ValidationResult Validate(object value, CultureInfo cultureInfo)
+        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
+        {
+            string email = string.Empty;
+            if (value != null)
+            {
+                email = value.ToString();
+            }
+            else
+                return new ValidationResult(false, " Адрес электронной почты не задан! ");
+            if (email.Contains("@") && email.Contains(".")) 
+            {
+                return new ValidationResult(true, null);
+            }
+            else
+            {
+                return new ValidationResult(false, "Адрес электронной почты должен содеражть сиволы @ и . точки \nШаблон адреса: adres@mymail.com");
+            }
+        }
+    }
+}

+ 18 - 1
LR1/Workers.xaml

@@ -6,6 +6,7 @@
       xmlns:local="clr-namespace:LR1"
       xmlns:local="clr-namespace:LR1"
       xmlns:command="clr-namespace:LR1.Commands"
       xmlns:command="clr-namespace:LR1.Commands"
       xmlns:core ="clr-namespace:LR1.Model"
       xmlns:core ="clr-namespace:LR1.Model"
+      xmlns:rule ="clr-namespace:LR1.ValidationRules"
       mc:Ignorable="d" 
       mc:Ignorable="d" 
       d:DesignHeight="500" d:DesignWidth="800"
       d:DesignHeight="500" d:DesignWidth="800"
       Title="Workers" Loaded="Page_Loaded">
       Title="Workers" Loaded="Page_Loaded">
@@ -58,6 +59,14 @@
         <DataGrid Margin="25,129,25,25" Name="DataGridEmployee" ItemsSource="{Binding}" AutoGenerateColumns="False" HorizontalAlignment="Left" 
         <DataGrid Margin="25,129,25,25" Name="DataGridEmployee" ItemsSource="{Binding}" AutoGenerateColumns="False" HorizontalAlignment="Left" 
                   MaxWidth="1000" MaxHeight="295" RowBackground="#FFE6D3EF" AlternatingRowBackground="#FC96CFD4" BorderBrush="#FF1F33EB"
                   MaxWidth="1000" MaxHeight="295" RowBackground="#FFE6D3EF" AlternatingRowBackground="#FC96CFD4" BorderBrush="#FF1F33EB"
                   BorderThickness="3" IsReadOnly="True" RowHeight="25" Cursor="Hand" CanUserAddRows="False" CanUserDeleteRows="False">
                   BorderThickness="3" IsReadOnly="True" RowHeight="25" Cursor="Hand" CanUserAddRows="False" CanUserDeleteRows="False">
+            <DataGrid.RowValidationErrorTemplate>
+                <ControlTemplate>
+                    <Grid Margin="0,-2,0,-2" ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}},Path=(Validation.Errors)[0].ErrorContent}">
+                        <Ellipse StrokeThickness="0" Fill="Red" Width="{TemplateBinding FontSize}" Height="{TemplateBinding FontSize}"/>
+                        <TextBlock Text="!" FontSize="{TemplateBinding FontSize}" FontWeight="Bold" Foreground="White" HorizontalAlignment="Center"/>
+                    </Grid>
+                </ControlTemplate>
+            </DataGrid.RowValidationErrorTemplate>
             <DataGrid.Columns>
             <DataGrid.Columns>
                 <DataGridTextColumn Header="ID" Width="25" Binding="{Binding ID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                 <DataGridTextColumn Header="ID" Width="25" Binding="{Binding ID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                 <DataGridTextColumn Header="Фамилия" Width="80" Binding="{Binding Surname, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                 <DataGridTextColumn Header="Фамилия" Width="80" Binding="{Binding Surname, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
@@ -70,7 +79,15 @@
                                         CellTemplate="{StaticResource DataTemplate}" 
                                         CellTemplate="{StaticResource DataTemplate}" 
                                         CellEditingTemplate="{StaticResource EditingDataTemplate}"/>
                                         CellEditingTemplate="{StaticResource EditingDataTemplate}"/>
                 <DataGridTextColumn Header="Телефон" Width="120" Binding="{Binding Telephone, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                 <DataGridTextColumn Header="Телефон" Width="120" Binding="{Binding Telephone, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
-                <DataGridTextColumn Header="Электронная почта" Width="*" Binding="{Binding Email, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
+                <DataGridTextColumn Header="Электронная почта" Width="*" EditingElementStyle="{StaticResource errorStyle}">
+                    <DataGridTextColumn.Binding >
+                        <Binding Path="Email" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" ValidatesOnExceptions ="True" >
+                            <Binding.ValidationRules>
+                                <rule:EmailRule />
+                            </Binding.ValidationRules>
+                        </Binding>
+                    </DataGridTextColumn.Binding>
+                </DataGridTextColumn>
             </DataGrid.Columns>
             </DataGrid.Columns>
         </DataGrid>
         </DataGrid>
         <Label Style="{DynamicResource LabaelStyle}" Content="Список сотрудников" HorizontalAlignment="Left" Margin="25,90,0,0" VerticalAlignment="Top"/>
         <Label Style="{DynamicResource LabaelStyle}" Content="Список сотрудников" HorizontalAlignment="Left" Margin="25,90,0,0" VerticalAlignment="Top"/>