完善主體資料,免費贈送VIP會員!
    * 主體類型
    * 企業(yè)名稱
    * 信用代碼
    * 所在行業(yè)
    * 企業(yè)規(guī)模
    * 所在職位
    * 姓名
    * 所在行業(yè)
    * 學歷
    * 工作性質
    請先選擇行業(yè)
    您還可以選擇以下福利:
    行業(yè)福利,領完即止!

    下載app免費領取會員

    NULL

    ad.jpg

    二次開發(fā)教程:WPF 使用INotifyDataErrorInfo小例子

    發(fā)布于:2019-07-25 15:27:58

    網友投稿

    更多

    <Window x:Class="ErrorsTest.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:ErrorsTest"

            mc:Ignorable="d"

            Title="MainWindow" Height="268.884" Width="411.588">    

        <Window.Resources>       

            <Style TargetType="TextBox">

                <Style.Triggers>

                    <Trigger Property="Validation.HasError" Value="true">

                        <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=(Validation.Errors)[0].ErrorContent}"/>

                    </Trigger>

                </Style.Triggers>

            </Style>

        </Window.Resources>

        <Grid>

            <TextBox x:Name="textBox" Text="{Binding Path= N,ValidatesOnNotifyDataErrors=True}"  

                     HorizontalAlignment="Left" Height="23" Margin="97,92,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" >

            </TextBox>

            <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="217,158,0,0" 

                    VerticalAlignment="Top" Width="75"/>

        </Grid>

    </Window>




        public partial class MainWindow : Window

        {

            public MainWindow()

            {

                InitializeComponent();

                this.DataContext = new ViewModel();

            }

        }


        public class ViewModel : INotifyDataErrorInfo,INotifyPropertyChanged

        {

            private Dictionary<string, List<string>> _errors = new Dictionary<string, List<string>>();

            private int n = 0;

            public int N

            {

                get

                {

                    return n;

                }

                set

                {

                    List<string> te = new List<string>();

                    if(value<0)

                    {

                        te.Add("不能小于0!");

                    }

                    if(value%2!=0)

                    {

                        te.Add("不能位奇數!");

                    }

                    SetErrors("N", te);

                    n = value;

                }

            }


            public bool HasErrors

            {

                get

                {

                    return _errors.Count != 0;

                }

            }


            public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;

            public event PropertyChangedEventHandler PropertyChanged;

            private void RaiseNotifyPropertyChanged(string name)

            {

                if(null!=PropertyChanged)

                {

                    PropertyChanged(this, new PropertyChangedEventArgs(name));

                }

            }

            private void SetErrors(string name,List<string> errors)

            {

                if (errors.Count == 0)

                {

                    errors.Remove(name);

                }

                else

                {

                    if (_errors.ContainsKey(name))

                    {

                        _errors[name] = errors;

                    }

                    else

                    {

                        _errors.Add(name, errors);

                    }

                }

                RaiseErrorsChanged(name);

            }

            private void RaiseErrorsChanged(string name)

            {

                if(ErrorsChanged!=null)

                {

                    ErrorsChanged(this, new DataErrorsChangedEventArgs(name));

                }

            }


            public IEnumerable GetErrors(string propertyName)

            {

                return _errors[propertyName];

            }

        }

    本文版權歸腿腿教學網及原創(chuàng)作者所有,未經授權,謝絕轉載。

    未標題-1.jpg

    上一篇:二次開發(fā)教程:Revit開發(fā)自定義警告

    下一篇:二次開發(fā)教程:C#DynamicObject和ExpandoObject

    主站蜘蛛池模板: 精品一区二区AV天堂| AV无码精品一区二区三区宅噜噜| 国产在线精品一区二区不卡麻豆| 国产精品亚洲不卡一区二区三区| 日本在线不卡一区| 国产一区二区三区免费在线观看| 中文字幕一区二区三区有限公司 | 国产美女一区二区三区| 红杏亚洲影院一区二区三区| 国产成人高清亚洲一区久久| 蜜桃无码一区二区三区| 亚洲熟女一区二区三区| 日韩精品电影一区亚洲| 中文字幕一区二区三区人妻少妇| 午夜影视日本亚洲欧洲精品一区| 国产一区二区视频在线观看 | 中文字幕日韩一区| 精品视频一区二区三区免费| 久久精品综合一区二区三区| 亚洲国产一区二区三区| 国产一区二区三区美女| 精品一区二区三区免费毛片| 国产SUV精品一区二区88L| 国产精品区AV一区二区| 久久婷婷色综合一区二区| 国产一区二区三区无码免费| 无码人妻精品一区二| 美女一区二区三区| 无码人妻精品一区二区三区久久久| 精品国产伦一区二区三区在线观看 | 一区二区三区中文字幕| 国产乱码精品一区二区三区中文| 亚洲色大成网站www永久一区| 一区二区三区视频在线| 亚洲国产一区二区三区在线观看| 91在线视频一区| 日本一区二区三区在线看| 成人国内精品久久久久一区| 亚洲熟女少妇一区二区| 韩国资源视频一区二区三区| 中文字幕亚洲乱码熟女一区二区 |