wcf 简单实践 数据绑定 数据校验

1.概要

1.1 说明

数据校验,如果数据不合适,有提示。

1.2 要点

  • class User : IDataErrorInfo
  • this.DataContext = user;
  • <Window.Resources>
  • <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors)[0].ErrorContent}"></Setter>
  • Text="{Binding Path=Name,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}"

2.代码

2.1 xaml

复制代码
<Window x:Class="WpfApp7.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:WpfApp7"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:User x:Key="usecheck"></local:User>
        <Style TargetType="TextBox">
            <Setter Property="Background" Value="#ddd"></Setter>
            <Setter Property="Foreground" Value="Red"></Setter>
            <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors)[0].ErrorContent}"></Setter>
        </Style>
    </Window.Resources>
    <StackPanel>
        <StackPanel.DataContext>
            <Binding Source="{StaticResource usecheck}"></Binding>
        </StackPanel.DataContext>
        <WrapPanel>
            <TextBlock Text="姓名"></TextBlock>
            <TextBox Name="Name" Text="{Binding Path=Name,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}" Width="200"></TextBox>
        </WrapPanel>
        <WrapPanel>
            <TextBlock Text="年龄"></TextBlock>
            <TextBox Name="Age" Text="{Binding Path=Age,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}" Width="200"></TextBox>
        </WrapPanel>
    </StackPanel>
</Window>

2.2 code

复制代码
using System.ComponentModel;
using System.Windows;

namespace WpfApp7
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        User user = new User();
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = user;
        }
    }
    class User : IDataErrorInfo
    {
        public User()
        {
            this.Name = "dd";
            this.Age = 20;
        }

        public string this[string columnName] {
            get {
                string ret = null;
                switch (columnName)
                {
                    case "Name":
                        if (this.Name.Length <2|| this.Name.Length>3)
                        {
                            return "不合格Name";
                        }
                        break;
                    case "Age":
                        if(this.Age<0|| this.Age > 200)
                        {
                            return "不合格Age";
                        }
                        
                        break;

                }
                return null;
            }
        }

        public String Name { get; set; }
        public int Age { get; set; } = 0;

        public string Error => throw new NotImplementedException();
    }
}

3.试验结果

相关推荐
共享ui设计和前端开发人才4 小时前
UI前端大数据处理安全性保障:数据加密与隐私保护策略
ui
ui设计前端开发老司机4 小时前
UI前端大数据处理性能瓶颈突破:分布式计算框架的应用
ui
前端开发与ui设计的老司机4 小时前
UI前端与数字孪生结合实践探索:智慧物流的货物追踪与配送优化
前端·ui
UI设计和前端开发从业者8 小时前
从UI设计到数字孪生实战应用:构建智慧金融的智能风控平台
ui·金融
✎ ﹏梦醒͜ღ҉繁华落℘10 小时前
WPF学习(四)
学习·wpf
zzyzxb11 小时前
WPF中依赖属性和附加属性
wpf
✎ ﹏梦醒͜ღ҉繁华落℘11 小时前
WPF学习(动画)
学习·wpf
ui设计前端开发老司机16 小时前
数字孪生技术为UI前端提供全面支持:实现产品的可视化配置与定制
ui
X_StarX17 小时前
【Unity笔记01】基于单例模式的简单UI框架
笔记·ui·unity·单例模式·游戏引擎·游戏开发·大学生
Clair-Sean1 天前
【交互设计】UI 与 UX 简介:从核心概念到行业实践
ui·ux