默认情况下,Validation.ErrorTemplate
在WPF只是一个小红色边框没有任何ToolTip
.
在Silverlight 4中,验证错误很好地开箱即用.
以下是Silverlight 4和WPF中出现的验证错误的比较
Silverlight 4
WPF
请注意WPF版本的平坦,无聊的外观与我认为的Silverlight外观相比.
WPF框架中是否存在任何类似的验证样式/模板,或者是否有人创建了很好的样式验证模板,如上面的Silverlight版本?或者我是否必须从头开始创建它们?
如果有人想尝试一下,可以使用以下代码重现上面的验证错误,适用于Silverlight和WPF
主窗口/ 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>
Run Code Online (Sandbox Code Playgroud)
主窗口/ 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;
} …
Run Code Online (Sandbox Code Playgroud) 我有一种情况,我使用ExceptionValidationRule使用wpf数据绑定和验证.
解决方案的另一部分invovles折叠一些面板并显示其他面板.
如果设置了验证异常 - 即UI在UI元素周围显示带有验证问题的红色边框,并且包含的面板已折叠,则仍会显示红色边框.这显然不是意味着什么?这有解决方法吗?任何人都知道这是否是设计的?
提供的最小代码示例(不是我的实际代码,但复制问题).创建一个新的WpfApplication(我称之为WpfDataBindingProblem).
window1的xaml如下:
<Window x:Class="WpfDataBindingProblem.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel Margin="5">
<StackPanel Name="panel1" Visibility="Visible" Margin="5">
<TextBox Name="DataBoundTextBox">
<Binding Path="TextValue">
<Binding.ValidationRules>
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox>
</StackPanel>
<StackPanel Name="panel2" Visibility="Collapsed" Margin="5">
<TextBlock>
The quick brown fox jumps over the lazy dog.
</TextBlock>
</StackPanel>
<Button Click="Button_Click" Margin="5">
Toggle panels
</Button>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
window1的代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using …
Run Code Online (Sandbox Code Playgroud)