快捷搜索:

在WPF中实现数据验证实现自定义的Popup提示

传统的WPF中集成了数据有效性的验证,不过我们在应用中发明应用这个功能在WPF中并不能呈现像Silverlight中那样的弹出式的Popup提示,只能以血色的边框提示有差错的数据发生,不能以人道化的要领给予你提示,这彷佛给了我们一个小小的遗憾。不过我们可以使用WPF强大年夜的自定义图形功能增补我们的遗憾。下面就以自定义的Popup为例来阐明假如给数据添加验证提示功能。这是如下的效果图:

紧张提示:

1、为了对特定的信息进行验证,我在这儿应用了数据类从IDataErrorInfo 接口来实现差错的设置事情。

public interface IDataErrorInfo{

// Properties

string Error { get; }

string this[string columnName] { get; }//差错详情 }

2、为了对应用WPF对发生的数据改变作出及时的反映,我们还得应用INotifyPropertyChanged接口以实现数据更新看护事故。

public interface INotifyPropertyChanged{

// Events

event PropertyChangedEventHandler PropertyChanged;}

3、着末我们还得对数据发生验证差错时,我们还得附加响应的差错事故处置惩罚,这里就得应用Validation类来附加处置惩罚事故了,响应的类型定义如下:

public static class Validation{

public static void AddErrorHandler(DependencyObject element, EventHandlerValidationErrorEventArgs> handler);//附加事故处置惩罚

public static bool GetHasError(DependencyObject element);//响应的工具是否有差错发生 }

详细实现:

1、 定义一个Popup图形化工具,因为为了应用可视化的设计,我们这儿没有采纳硬编码的要领来应用Poppup工具,而是采纳了硬编码节制Popup的IsOpen的属性来实现的。在主窗体的Xaml代码中我们定义了它的响应的样式,如下所示:

Popup

Popup x:Name="emailPopup" PopupAnimation="Scroll" Placement="Relative" Margin="20,0,0,40" AllowsTransparency="True" Width="175" HorizontalAlignment="Right" VerticalAlignment="Top" HorizontalOffset="200" VerticalOffset="-10">

Grid Height="47" Width="175" SnapsToDevicePixels="True">

Path Data="M12.166999,0.5 L143.167,0.5 C145.92843,0.50000003 148.167,2.7385762 148.167,5.5 L148.167,30.5 C148.167,33.261425 145.92843,35.5 143.167,35.5 L12.166999,35.5 11.81918,35.482437 11.834001,35.5 C10.619751,36.16069 10.9795,36.5625 8.1912529,37.482075 5.4030061,38.401646 3.063751,40.272026 0.5,41.667 2.2780275,39.667053 4.7294998,37 5.8340837,35.66716 6.662521,34.66753 6.9629115,32.753006 7.1582331,31.348595 L7.1991105,31.052473 7.1928148,31.011221 C7.1757442,30.843136 7.1669998,30.672588 7.1669995,30.5 L7.1669995,17.25 7.1669995,5.5 C7.1669998,2.7385762 9.405576,0.50000003 12.166999,0.5 z" Margin="1.333,0,0,1.833" Stretch="Fill" Stroke="#FF32E767" UseLayoutRounding="False">

Path.Fill>

LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

GradientStop Color="#FF5F5DB6" Offset="1"/>

GradientStop Color="#FF3F3BCA"/>

GradientStop Color="#FFACABE9" Offset="0.747"/>

LinearGradientBrush>

Path.Fill>

Path>

Path Data="M67.5,15.25 C70.750538,10.312179 121.33333,14.916667 148.25,14.75 148.41667,11.083333 149,6.0625 148.75,3.75 148.5,1.4375 145.43711,0.0625 144.5,0.25 149.46607,0.22253689 13.874996,-0.062023075 11.75,0.25 9.6250039,0.56202307 8.3747773,3.5006011 8.25,4.75 8.1252227,5.9993989 8.159157,10.23778 8.25,14.5 28,14.75 64.249462,20.187821 67.5,15.25 z" Margin="8,0,0,28.686" Stretch="Fill" Stroke="Black" UseLayoutRounding="False" StrokeThickness="0">

Path.Fill>

LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

GradientStop Color="#B2FFFFFF"/>

GradientStop Color="#33FFFFFF" Offset="1"/>

LinearGradientBrush>

Path.Fill>

Path>

TextBlock x:Name="errorTextBlock_Copy" TextWrapping="Wrap" Text="{Binding .}" Margin="11,3,3,11" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#FFF11818" Width="157" TextAlignment="Center"/>

Grid>

Popup>

您可能还会对下面的文章感兴趣: