WPF 中的错误提供程序

发布于 2024-08-11 07:01:04 字数 65 浏览 5 评论 0原文

我正在工具箱中查看 WPF 组件,但找不到 2005/2008 中出现的错误提供程序。

被移除了吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏至、离别 2024-08-18 07:01:04

ErrorProvider 是一个 Winforms 控件。 WPF 中没有类似的东西。但如果您创建一个 win forms 项目,您仍然可以在 Visual Studio 2008 中找到它。

您可能需要查看这篇有关 WPF 中的错误验证的文章。它对于如何处理验证提供了一些有用的建议和想法。

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.

倾听心声的旋律 2024-08-18 07:01:04

.NET 3.5 添加了对 IDataErrorInfo 的 WPF 支持:.NET 3.5 中的数据验证

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

林空鹿饮溪 2024-08-18 07:01:04

首先请原谅我评论如此古老的讨论,但这可能会有所帮助,因为我有完全相同的问题,西蒙的链接帮助我“从某件事开始”

我可以测试西蒙 P.史蒂文斯教程,但老实说我不喜欢它就这么多:

  • 使用responseTemplate 会使显示错误时的响应速度变慢。
  • 仅当同一类的规则始终相同时,这才有效(在我的例子中,我有一些数量,有时可能是负数,有时不是)。
  • 在国际化应用程序的情况下(在我的例子中),外部库无法访问包含翻译的资源,因此我无法设置适当的消息。

我认为使用 MVVM 非常适合管理任何情况:

我设置了 TextBox、BorderBrush 和 ToolTip,根据我的情况,我将隐藏/显示 ToolTip 和颜色边框:

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}"/>

代码隐藏(LostFocus = 留给习惯的人) WindowsForm) :

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

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

然后是 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");
                }
            }

当规则针对具体情况时非常有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文