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.试验结果

相关推荐
hqwest5 小时前
C#WPF实战出真汁07--【系统设置】--菜品类型设置
开发语言·c#·wpf·grid设计·stackpanel布局
界面开发小八哥12 小时前
DevExtreme Angular UI控件更新:引入全新严格类型配置组件
前端·ui·界面控件·angular.js·devexpress
hqwest15 小时前
C#WPF实战出真汁08--【消费开单】--餐桌面板展示
c#·wpf·ui设计·wpf界面设计
眠りたいです15 小时前
Qt音频播放器项目实践:文件过滤、元数据提取与动态歌词显示实现
c++·qt·ui·音视频·媒体·qt5·mime
orangapple15 小时前
WPF 打印报告图片大小的自适应(含完整示例与详解)
c#·wpf
Jerry说前后端1 天前
Android 移动端 UI 设计:前端常用设计原则总结
android·前端·ui
三千道应用题1 天前
WPF&C#超市管理系统(6)订单详情、顾客注册、商品销售排行查询和库存提示、LiveChat报表
开发语言·c#·wpf
传奇开心果编程2 天前
【传奇开心果系列】Flet框架实现的家庭记账本示例自定义模板
python·学习·ui·前端框架·自动化
公众号【林东笔记】获取资料2 天前
Adobe Photoshop 2024:软件安装包分享和详细安装教程
ui·adobe·photoshop
✎ ﹏梦醒͜ღ҉繁华落℘2 天前
开发WPF项目时遇到的问题总结
wpf