Error Provider in WPF

孤人 提交于 2020-01-14 10:33:31

问题


I am looking at WPF componenents in the toolbox but I cannot find the error provider that is present in 2005/2008.

Is it removed?


回答1:


the ErrorProvider is a Winforms control. There is no equivalent in WPF. But you will still be able to find in in visual studio 2008 if you create a win forms project.

You might want to take a look at this article on error validation in WPF. It has some useful suggestions and ideas for how to handle validation.




回答2:


.NET 3.5 added WPF support for IDataErrorInfo: Data validation in .NET 3.5.




回答3:


First excuse me for commenting a such old discussion, but this could help as I had exactly the same question, and Simon's link helped me to "begin with something"

I could test Simon P.Stevens tutorial, but honestly I didn't like it that much :

  • Using responseTemplate makes the response slower when displaying the error.
  • This works only if the rule is always the same for a same class (in my case I have some quantities, that sometimes can be negative, sometimes not).
  • In the case of an internationalised application(in my case), external libraries have no access to Resources where are translations, so I cannot set appropriated message.

I think using MVVM is very well adapted to manage any situation :

I set my TextBox, with a BorderBrush , and ToolTip, regarding my conditions I will Hide/Display ToolTip and color border :

XAML :

<TextBox x:Name="tbName" Grid.Column="1" Grid.Row="0" Margin="3" LostFocus="tbName_LostFocus" BorderBrush="{Binding BordertbName}"
                 Text="{Binding MonRepere.Nom}" ToolTipService.ToolTip="{Binding ErrorName}" ToolTipService.IsEnabled="{Binding ToolTipNameEnable}"/>

Code Behind (LostFocus = Leave for whom used to WindowsForm) :

private void tbName_LostFocus(object sender, RoutedEventArgs e)
    {
        if(tbName.Text=="")
        {
            this.mv.ErrorName = Properties.Resources.ErrorEmpty;

        }
        else
        {
            mv.ErrorName = "";
        }
    }

Then ViewModel :

private string errorName;
            public string ErrorName
            {
                get { return errorName; }
                set
                {
                    errorName = value;
                    if (value == "")
                    {
                        ToolTipNameEnable = false;
                        BordertbName = Brushes.Gray;
                    }
                    else
                    {
                        ToolTipNameEnable = true;
                        BordertbName = Brushes.Red;
                    }
                    this.NotifyPropertyChanged("ErrorName");
                }
            }
            private Brush bordertbName;
            public Brush BordertbName
            {
                get { return bordertbName; }
                set
                {
                    bordertbName = value;
                    this.NotifyPropertyChanged("BordertbName");
                }
            }
            private bool toolTipNameEnable;
            public bool ToolTipNameEnable
            {
                get { return toolTipNameEnable; }
                set
                {
                    toolTipNameEnable = value;
                    this.NotifyPropertyChanged("ToolTipNameEnable");
                }
            }

Just very useful when rules are specific regarding the situation.



来源:https://stackoverflow.com/questions/1701126/error-provider-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!