Error Provider in WPF
Asked Answered
D

3

6

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?

Draghound answered 9/11, 2009 at 13:59 Comment(0)
S
10

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.

Scamander answered 9/11, 2009 at 14:6 Comment(0)
S
3

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

Socher answered 9/11, 2009 at 14:7 Comment(0)
E
0

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.

Erlin answered 8/3, 2018 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.