WPF 中的验证错误样式,类似于 Silverlight
默认情况下,WPF 中的Validation.ErrorTemplate
只是一个红色小边框,没有任何ToolTip
。
在 Silverlight 4 中,验证错误的样式很好,开箱即用。
以下是 Silverlight 4 和 WPF 中发生的验证错误的比较
Silverlight 4
WPF
请注意,与我认为的很棒的外观相比,WPF 版本的外观非常扁平、无聊。银光。
WPF 框架中是否存在任何类似的验证样式/模板,或者是否有人创建了样式漂亮的验证模板(如上面的 Silverlight 版本)?或者我必须从头开始创建它们?
如果有人想尝试一下,可以使用以下代码重现上面的验证错误,适用于 Silverlight 和 WPF
MainWindow/MainPage.xaml
<StackPanel Orientation="Horizontal" Margin="10" VerticalAlignment="Top">
<TextBox Text="{Binding Path=TextProperty, Mode=TwoWay, ValidatesOnExceptions=True}"/>
<Button Content="Tab To Me..." Margin="20,0,0,0"/>
</StackPanel>
MainWindow/MainPage.xaml.cs
public MainWindow/MainPage()
{
InitializeComponent();
this.DataContext = this;
}
private string _textProperty;
public string TextProperty
{
get { return _textProperty; }
set
{
if (value.Length > 5)
{
throw new Exception("Too many characters");
}
_textProperty = value;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我研究了验证错误模板的 Silverlight 版本,并创建了它的 WPF 版本,如下所示
在帖子底部添加了一个动画 GIF,但完成后我发现它可能很烦人,因为其中移动的鼠标。让我知道是否应该删除它..:)
我使用了
MultiBinding
和BooleanOrConverter
来显示“工具提示错误”,当TextBox
具有键盘焦点或鼠标位于右上角。对于淡入动画,我使用了DoubleAnimation
作为Opacity
和ThicknessAnimation
以及BackEase
/Margin
的 >EaseOutEasingFunction
可像这样使用
errorTemplateSilverlightStyle
BooleanOrConverter
I studied the Silverlight version of the Validation Error Template and created a WPF version of it which looks like this
Added an animated GIF at the bottom of the post but after I finished it I noticed that it might be annoying because of the moving mouse in it. Let me know if I should remove it.. :)
I used a
MultiBinding
with aBooleanOrConverter
to show the "tooltip-error" when theTextBox
has Keyboard focus or the Mouse is over the upper right corner. For the fade-in animation I used aDoubleAnimation
for theOpacity
and aThicknessAnimation
with aBackEase
/EaseOut
EasingFunction
for theMargin
Useable like this
errorTemplateSilverlightStyle
BooleanOrConverter
这个答案只是扩展了 Fredrik Hedblad 的出色答案。作为 WPF 和 XAML 的新手,Fredrik 的答案充当了定义我希望如何在应用程序中显示验证错误的跳板。虽然下面的 XAML 对我有用,但它是一项正在进行的工作。我还没有完全测试它,并且我很容易承认我无法完全解释每个标签。有了这些警告,我希望这对其他人有用。
而动画 TextBlock< /a> 是一个很好的方法,它有两个我想解决的缺点。
这是我完成开发的对话框。
如您所见,有两个 TextBox 需要验证的控件。两者都相对靠近窗口的右边缘,因此很长的错误消息可能会被裁剪掉。请注意第二个 TextBox 有一个浏览按钮,我不想在发生错误时隐藏该按钮。
下面是使用我的实现时出现的验证错误。
从功能上讲,它与 Fredrik 的实现非常相似。如果 文本框 具有焦点,错误将可见。一旦失去焦点,错误就会消失。如果用户将鼠标悬停在 toolTipCorner 上,无论 TextBox 是否具有焦点。还有一些外观上的变化,例如 toolTipCorner 增大了 50%(9 像素与 6 像素)。
当然,明显的区别是我的实现使用 弹出窗口显示错误。这解决了第一个缺点,因为 Popup 在自己的窗口中显示其内容,因此它不受对话框边框的限制。但是,使用 Popup 确实提出了一些需要克服的挑战。
幸运的是,这两个挑战都已得到解决。
这是代码。欢迎评论和完善!
(注意:这需要 EXPRESSION BLEND 4 System.Windows.Interactivity 程序集)< /强>
This answer merely expands on Fredrik Hedblad's excellent answer. Being new to WPF and XAML, Fredrik's answer served as a springboard for defining how I wanted validation errors to be displayed in my application. While the XAML below works for me, it is a work in progress. I have not fully tested it, and I will readily admit that I cannot fully explain every tag. With those caveats, I hope this proves useful to others.
While the animated TextBlock is a fine approach, it has two shortcomings that I wanted to address.
Here is the dialog around which I've done my development.
As you can see, there are two TextBox controls that need to be validated. Both are relatively close to the right edge of the window, so long error messages would likely be cropped. And notice that the second TextBox has a Browse button that I don't want hidden in the event of an error.
So here's what a validation error looks like using my implementation.
Functionally, it is very similar to Fredrik's implementation. If the TextBox has focus, the error will be visible. Once it loses focus, the error disappears. If the user hovers the mouse over the toolTipCorner, the error will appear regardless of whether the TextBox has focus or not. There are a few cosmetic changes as well, such as the toolTipCorner being 50% larger (9 pixels vs. 6 pixels).
The obvious difference, of course, is that my implementation uses a Popup to display the error. This solves the first shortcoming because the Popup displays its contents in its own window, so it is not constrained by the dialog's borders. However, using a Popup did present a couple challenges to overcome.
Fortunately, both of these challenges have been addressed.
Here's the code. Comments and refinements are welcome!
(NOTE: THIS REQUIRES THE EXPRESSION BLEND 4 System.Windows.Interactivity ASSEMBLY)
我在其中一个项目中创建了自定义错误装饰器,以在文本框下方显示错误装饰器,其中包含错误消息。您只需在文本框默认样式中设置属性“Validation.ErrorTemplate”,您可以将其保留在应用程序资源中,以便将其应用于应用程序中的所有文本框。
注意:我在这里使用了一些画笔,请将其替换为您自己的装饰信息所需的画笔。也许这会有所帮助:
I have created my custom error adorner in one of the projects to show the error adorner just below my textbox with error message in it. You just need to set the property "Validation.ErrorTemplate" in your textbox default style which you can keep in your app resources so that it get applied to all the textboxes in your application.
Note: I have used some brushes here, replace it with your own set of brushes which you want for your adorner messgae. May be this can be of some help :
当我尝试将其应用到我正在开发的 wpf 项目时,我遇到了一个问题。如果您在尝试运行项目时遇到以下问题:
“PresentationFramework.dll 中出现类型为‘System.Windows.Markup.XamlParseException’的异常,但未在用户代码中处理”,
您需要创建一个实例资源中的 booleanOrConverter 类(在 app.xaml 中):
另外不要忘记将命名空间添加到文件顶部(在应用程序标记中):
xmlns:validators =“clr命名空间:ParcelRatesViewModel.Validators;程序集= ParcelRatesViewModel”
I ran into an issue with this when trying to apply it to a wpf project I'm working on. If you're having the following issue when you try to run the project:
"An exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll but was not handled in user code"
You need to create an instance of the booleanOrConverter class in your resources (within app.xaml):
Also don't forget to add the namespace to the top of the file (in the application tag):
xmlns:validators="clr-namespace:ParcelRatesViewModel.Validators;assembly=ParcelRatesViewModel"