Reputation: 934
I just implement the IDataErrorInfo
and I always wonder why public string Error => null;
is neccesary for the interface.
I implement it as following:
//this works fine
public string this[string propertyName] => propertyName switch
{
//do my check
nameof(my property) => thing i want to validate ? error message : nothing
}
//what to do with this?
//0 references
public string Error => null;
Can anybody explain me please why do I need it, or why does it even exist?
Thank you very much :-)
Upvotes: 1
Views: 91
Reputation: 295
MS Spec says it "Gets an error message indicating what is wrong with this object." I can't see anything in WPF or MVC 4.X that actually uses it automatically. But you could have a summary string that would have to be manually retrieved and displayed.
see below
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Style x:Key="validationTooltip" TargetType="{x:Type TextBox}" >
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type TextBox}" x:Key="asterisk" BasedOn="{StaticResource validationTooltip}" >
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Grid ToolTip="{Binding ElementName=customAdorner,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" >
<TextBlock Foreground="Red" FontSize="16pt" HorizontalAlignment="Right" VerticalAlignment="Center"
Margin="0,0,10,0" FontWeight="Bold">*</TextBlock>
<Border BorderBrush="Red" BorderThickness="1" Margin="6"
ToolTip="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}">
<AdornedElementPlaceholder Name="customAdorner" />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid Name="grdData">
<TextBox Style="{StaticResource asterisk}" Width="550" Height="44" Name="txtValue" Text="{Binding Path=SomeProperty, ValidatesOnDataErrors=True}" />
<TextBox Text="{Binding Path=Error, Mode=OneWay}" HorizontalAlignment="Left" Height="44" Margin="122,257,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="550"/>
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var item= new MyClass() { SomeProperty = "Testing" };
grdData.DataContext = item;
}
}
public class MyClass : IDataErrorInfo
{
public string SomeProperty { get; set; }
public string this[string columnName] => "Some Specific Error";
public string Error => "Something is wrong";
}
Upvotes: 2
Reputation: 273284
The this[]
indexer property lists the errors per property.
string Error
is "An error message indicating what is wrong with this object".
So it is for any problem that is not directly related to a specific property.
It is used far less often, returning null
is customary.
Upvotes: 3