WPF验证错误事件不会触发 [英] WPF- validation error event doesn't fire
问题描述
我想我已经阅读了所有相关的文章,但没有帮助。
我试图启用/禁用的保存按钮datagrid
以错误状态显示,但没有成功。
这是我的代码:
承包商:
AddHandler(Validation.ErrorEvent,new RoutedEventHandler(OnErrorEvent));
XAML:
code>< Page
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft。
xmlns:d =http://schemas.microsoft。 com / expression / blend / 2008
xmlns:col =clr-namespace:System.Collections; assembly = mscorlib
xmlns:local =clr-namespace:Metsuka_APPx:Class =Metsuka_APP .MichlolimManagment
mc:Ignorable =d
d:DesignHeight =500d:DesignWidth =500
标题=MichlolimManagment
x:Name =Michlolim_Managment Validation.Error = Michlolim_Managment_Error >
< Page.Resources>
< DataGrid x:Name =AGAFIMDataGridVerticalAlignment =CenterRowEditEnding =rowEditEndingMargin =10FlowDirection =RightToLeftHeight =340
AutoGenerateColumns = FalseEnableRowVirtualization =True
ItemsSource ={Binding Source = {StaticResource aGAFIMViewSource}}Grid.Row =1
RowDetailsVisibilityMode =VisibleWhenSelected
ScrollViewer.CanContentScroll =True
ScrollViewer.VerticalScrollBarVisibility =Auto
HorizontalGridLinesBrush =Silver
VerticalGridLinesBrush =Silver>
< DataGrid.Resources>
< Style x:Key =errorStyleTargetType ={x:Type TextBox}>
< Setter Property =PaddingValue = - 2/>
< Style.Triggers>
< Trigger Property =Validation.HasErrorValue =True>
< Setter Property =BackgroundValue =Red/>
< Setter Property =ToolTip
Value ={Binding RelativeSource = {RelativeSource Self},
Path =(Validation.Errors)[0] .ErrorContent}/>
< / Trigger>
< /Style.Triggers>
< / Style>
< /DataGrid.Resources>
< DataGrid.Columns>
< DataGridTextColumn x:Name =agaf_nameColumnHeader =nameWidth =*>
< DataGridTextColumn.Binding>
< Binding Path =agaf_nameNotifyOnValidationError =True>
< Binding.ValidationRules>
< local:MichlolimValidationRule ValidationStep =UpdatedValue/>
< /Binding.ValidationRules>
< / Binding>
< /DataGridTextColumn.Binding>
< / DataGridTextColumn>
< /DataGrid.Columns>
< DataGrid.RowValidationErrorTemplate>
< ControlTemplate>
< Grid Margin =0,-2,0,-2
ToolTip ={Binding RelativeSource = {RelativeSource
FindAncestor,AncestorType = {x:Type DataGridRow}},
Path =(Validation.Errors)[0] .ErrorContent}>
< Ellipse StrokeThickness =0Fill =Red
Width ={TemplateBinding FontSize}
Height ={TemplateBinding FontSize}/>
< TextBlock Text =! FontSize ={TemplateBinding FontSize}
FontWeight =BoldForeground =White
HorizontalAlignment =Center/>
< / Grid>
< / ControlTemplate>
< /DataGrid.RowValidationErrorTemplate>
< / DataGrid>
代码:
private int errorCount;
private void OnErrorEvent(object sender,RoutedEventArgs e)
{
var validationEventArgs = e作为ValidationErrorEventArgs;
if(validationEventArgs == null)
throw new Exception(Unexpected event args);
switch(validationEventArgs.Action)
{
case ValidationErrorEventAction.Added:
{
errorCount ++;打破;
}
case ValidationErrorEventAction.Removed:
{
errorCount--;打破;
}
默认值:
{
抛出新异常(未知操作);
}
}
btnSavePop.IsEnabled = errorCount == 0;
}
但OnErrorEvent
永远不会触发 - 任何想法为什么?
属性
<$已经在您的XAML中的p $ p>
Validation.Error =Michlolim_Managment_Error
在窗口级别上为错误事件设置一个处理程序,但是在片段中的代码中未定义的方法。您的AddHandler调用看起来很好,但是,根据构造函数中的位置,它可能会被XAML事件处理程序定义覆盖。
可能不相关,但您可能要更改
(Validation.Errors)[0] .ErrorContent
to
(Validation.Errors)/ ErrorContent $ b您的ToolTip绑定中的$ b
,因为前者会导致绑定错误,如果没有错误。不幸的是,它仍然包含在文档示例中...
i think i have read already all related articles but non of them help..
im trying to enable/disable a save button of datagrid
by the error state- but with no success.
this is my code:
contractor:
AddHandler(Validation.ErrorEvent, new RoutedEventHandler(OnErrorEvent));
XAML:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
xmlns:local="clr-namespace:Metsuka_APP" x:Class="Metsuka_APP.MichlolimManagment"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="500"
Title="MichlolimManagment"
x:Name="Michlolim_Managment" Validation.Error="Michlolim_Managment_Error">
<Page.Resources>
<DataGrid x:Name="AGAFIMDataGrid" VerticalAlignment="Center" RowEditEnding="rowEditEnding" Margin="10" FlowDirection="RightToLeft" Height="340"
AutoGenerateColumns="False" EnableRowVirtualization="True"
ItemsSource="{Binding Source={StaticResource aGAFIMViewSource}}" Grid.Row="1"
RowDetailsVisibilityMode="VisibleWhenSelected"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
HorizontalGridLinesBrush="Silver"
VerticalGridLinesBrush="Silver">
<DataGrid.Resources>
<Style x:Key="errorStyle" TargetType="{x:Type TextBox}">
<Setter Property="Padding" Value="-2"/>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="Red"/>
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn x:Name="agaf_nameColumn" Header="name" Width="*">
<DataGridTextColumn.Binding>
<Binding Path="agaf_name" NotifyOnValidationError="True" >
<Binding.ValidationRules>
<local:MichlolimValidationRule ValidationStep="UpdatedValue"/>
</Binding.ValidationRules>
</Binding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
</DataGrid.Columns>
<DataGrid.RowValidationErrorTemplate>
<ControlTemplate>
<Grid Margin="0,-2,0,-2"
ToolTip="{Binding RelativeSource={RelativeSource
FindAncestor, AncestorType={x:Type DataGridRow}},
Path=(Validation.Errors)[0].ErrorContent}">
<Ellipse StrokeThickness="0" Fill="Red"
Width="{TemplateBinding FontSize}"
Height="{TemplateBinding FontSize}" />
<TextBlock Text="!" FontSize="{TemplateBinding FontSize}"
FontWeight="Bold" Foreground="White"
HorizontalAlignment="Center" />
</Grid>
</ControlTemplate>
</DataGrid.RowValidationErrorTemplate>
</DataGrid>
code behind:
private int errorCount;
private void OnErrorEvent(object sender, RoutedEventArgs e)
{
var validationEventArgs = e as ValidationErrorEventArgs;
if (validationEventArgs == null)
throw new Exception("Unexpected event args");
switch (validationEventArgs.Action)
{
case ValidationErrorEventAction.Added:
{
errorCount++; break;
}
case ValidationErrorEventAction.Removed:
{
errorCount--; break;
}
default:
{
throw new Exception("Unknown action");
}
}
btnSavePop.IsEnabled = errorCount == 0;
}
but the "OnErrorEvent"
never fires- any idea why?
The attribute
Validation.Error="Michlolim_Managment_Error"
in your XAML already sets a handler for the error event on the window level, however, to a method which isn't defined in your code behind fragment. Your AddHandler call looks ok, but, depending on where it is in the constructor, it might get overridden by the XAML event handler definition.
Probably not related, but you might want to change
(Validation.Errors)[0].ErrorContent
to
(Validation.Errors)/ErrorContent
in your ToolTip bindings, since the former causes binding errors if there are no errors. Unfortunately, it is still contained in the documentation samples...
这篇关于WPF验证错误事件不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!