binding validation

main
syneffort 2 years ago
parent ea8bc791de
commit 5e4f2e6fdc
  1. 9
      PacticeSolution/BindingValidationRuleSample/App.xaml
  2. 17
      PacticeSolution/BindingValidationRuleSample/App.xaml.cs
  3. 10
      PacticeSolution/BindingValidationRuleSample/AssemblyInfo.cs
  4. 10
      PacticeSolution/BindingValidationRuleSample/BindingValidationRuleSample.csproj
  5. 64
      PacticeSolution/BindingValidationRuleSample/MainWindow.xaml
  6. 28
      PacticeSolution/BindingValidationRuleSample/MainWindow.xaml.cs
  7. 66
      PacticeSolution/BindingValidationRuleSample/Model/Student.cs
  8. 28
      PacticeSolution/BindingValidationRuleSample/Validation/AgeRangeException.cs
  9. 6
      PacticeSolution/PacticeSolution.sln
  10. 18
      PacticeSolution/TabControlSample/MainWindow.xaml

@ -0,0 +1,9 @@
<Application x:Class="BindingValidationRuleSample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BindingValidationRuleSample"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace BindingValidationRuleSample
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

@ -0,0 +1,10 @@
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>

@ -0,0 +1,64 @@
<Window x:Class="BindingValidationRuleSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:BindingValidationRuleSample"
xmlns:model="clr-namespace:BindingValidationRuleSample.Model"
xmlns:validation="clr-namespace:BindingValidationRuleSample.Validation"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="300">
<Window.DataContext>
<model:Student/>
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10,2"
Text="Name: "/>
<TextBlock Grid.Column="0" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10,2"
Text="Age: "/>
<TextBlock Grid.Column="0" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10,2"
Text="Phone Number: "/>
<TextBox Grid.Column="1" Grid.Row="0" Margin="5"
Text="{Binding Name, Mode=TwoWay}"/>
<TextBox x:Name="tbxAge" Grid.Column="1" Grid.Row="1" Margin="5"
ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}">
<TextBox.Text>
<Binding Path="Age"
Mode="TwoWay"
UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<validation:AgeRangeException/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<TextBox Grid.Column="1" Grid.Row="2" Margin="5" Foreground="Blue"
Text="{Binding PhoneNumber, Mode=TwoWay}"/>
<Grid Grid.Column="0" Grid.Row="4" Grid.ColumnSpan="2" Height="30">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Column="2" Content="OK" Margin="5 3"
Foreground="{Binding ElementName=tbxAge, Path=Foreground}"/>
<Button Grid.Column="3" Content="Cancel" Margin="5 3"/>
</Grid>
</Grid>
</Window>

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 BindingValidationRuleSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}

@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BindingValidationRuleSample.Model
{
public class Student : INotifyPropertyChanged
{
private string _name;
private string _age;
private string _phoneNumber;
public event PropertyChangedEventHandler? PropertyChanged;
public Student(string name, string age, string number)
{
}
public Student() : this("Name", "20", "123-456-7890")
{
}
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged(nameof(this.Name));
}
}
public string Age
{
get { return _age; }
set
{
_age = value;
OnPropertyChanged(nameof(this.Age));
}
}
public string PhoneNumber
{
get { return _phoneNumber; }
set
{
_phoneNumber = value;
OnPropertyChanged(nameof(this.PhoneNumber));
}
}
private void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged == null)
return;
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
namespace BindingValidationRuleSample.Validation
{
public class AgeRangeException : ValidationRule
{
public int MinAge { get; set; } = 0;
public int MaxAge { get; set; } = 150;
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
int age = 0;
if (!int.TryParse(value.ToString(), out age))
return new ValidationResult(false, "Wrong value.");
if (age >= this.MinAge && age <= this.MaxAge)
return new ValidationResult(true, null);
else
return new ValidationResult(false, $"Valid age range is {this.MinAge}~{this.MaxAge}");
}
}
}

@ -53,6 +53,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TabControlSample", "TabCont
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProgressBarSample", "ProgressBarSample\ProgressBarSample.csproj", "{EB304FB7-67EA-467E-9BE4-E55187415CAB}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProgressBarSample", "ProgressBarSample\ProgressBarSample.csproj", "{EB304FB7-67EA-467E-9BE4-E55187415CAB}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BindingValidationRuleSample", "BindingValidationRuleSample\BindingValidationRuleSample.csproj", "{B328C31A-8C85-4A48-A12F-26E11C37A15C}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -159,6 +161,10 @@ Global
{EB304FB7-67EA-467E-9BE4-E55187415CAB}.Debug|Any CPU.Build.0 = Debug|Any CPU {EB304FB7-67EA-467E-9BE4-E55187415CAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EB304FB7-67EA-467E-9BE4-E55187415CAB}.Release|Any CPU.ActiveCfg = Release|Any CPU {EB304FB7-67EA-467E-9BE4-E55187415CAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EB304FB7-67EA-467E-9BE4-E55187415CAB}.Release|Any CPU.Build.0 = Release|Any CPU {EB304FB7-67EA-467E-9BE4-E55187415CAB}.Release|Any CPU.Build.0 = Release|Any CPU
{B328C31A-8C85-4A48-A12F-26E11C37A15C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B328C31A-8C85-4A48-A12F-26E11C37A15C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B328C31A-8C85-4A48-A12F-26E11C37A15C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B328C31A-8C85-4A48-A12F-26E11C37A15C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

@ -7,7 +7,23 @@
mc:Ignorable="d" mc:Ignorable="d"
Title="MainWindow" Height="450" Width="400"> Title="MainWindow" Height="450" Width="400">
<Grid Margin="10"> <Grid Margin="10">
<TabControl> <TabControl TabStripPlacement="Left">
<TabControl.Resources>
<Style TargetType="{x:Type TabItem}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<ContentPresenter Margin="0 5 0 5" Content="{TemplateBinding Content}">
<ContentPresenter.LayoutTransform>
<RotateTransform Angle="270"/>
</ContentPresenter.LayoutTransform>
</ContentPresenter>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TabControl.Resources>
<TabItem> <TabItem>
<TabItem.Header> <TabItem.Header>
<StackPanel Orientation="Horizontal"> <StackPanel Orientation="Horizontal">

Loading…
Cancel
Save