gpt4 book ai didi

c# - WPF IDataErrorInfo 问题

转载 作者:太空宇宙 更新时间:2023-11-03 12:54:09 29 4
gpt4 key购买 nike

我似乎找不到关于如何设置它的简单说明,有人可以帮忙吗?

我几乎阅读了所有教程,但每个教程都没有完全解释,我的问题是我已经编写了一些代码,但我不确定要在 MainWindow.xamls.cs< 中编写什么 以及如何使验证工作。

public class Person : IDataErrorInfo
{
public string Fname { get; set; }
public string Lname { get; set; }
public string Error
{
get { return ""; }
}

public string this[string columnName]
{
get
{
string result = null;
if (columnName == "Fname")
{
if (string.IsNullOrEmpty(Fname))
{
result = "First name is required.";
return result;
}
string st = @"!|@|#|\$|%|\?|\>|\<|\*";
if (Regex.IsMatch(Fname, st))
{
result = "Contains invalid characters.";
return result;
}
}
if (columnName == "Lname")
{
if (string.IsNullOrEmpty(Lname))
{
result = "Cannot be empty.";
return result;
}
}
return null;
}
}
}

XAML

<Window x:Class="WpfApplication2.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:WpfApplication2"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ControlTemplate x:Key="eTemplate">
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Right" Foreground="Blue" FontSize="13" Text="{Binding ElementName=adorned,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" >
</TextBlock>
<Border BorderBrush="Red" BorderThickness="2">
<AdornedElementPlaceholder x:Name="adorned"/>
</Border>
</DockPanel>
</ControlTemplate>
</Window.Resources>


<Grid>
<TextBox Height="23" Validation.ErrorTemplate="{StaticResource ResourceKey=eTemplate}" HorizontalAlignment="Left" Margin="198,71,0,0" Name="Fname" VerticalAlignment="Top" Width="120" FontSize="15">
<TextBox.Text>
<Binding Path="Fname" ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged"/>
</TextBox.Text>
</TextBox>
<TextBox Height="23" Validation.ErrorTemplate="{StaticResource ResourceKey=eTemplate}" HorizontalAlignment="Left" Margin="198,130,0,0" Name="Lname" VerticalAlignment="Top" Width="120" FontSize="15">
<TextBox.Text>
<Binding Path="Lname" ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged"/>
</TextBox.Text>
</TextBox>
<Label Content="FirstName" FontSize="14" Height="28" HorizontalAlignment="Left" Margin="114,71,0,0" Name="FirstName" VerticalAlignment="Top" FontFamily="Consolas" RenderTransformOrigin="0.063,0.607" Width="84"/>
<Label Content="LastName" FontSize="14" Height="28" HorizontalAlignment="Left" Margin="114,130,0,0" Name="LastName" VerticalAlignment="Top" FontFamily="Consolas" Width="79"/>
<Button x:Name="Add" Content="test" HorizontalAlignment="Left" Margin="198,186,0,0" VerticalAlignment="Top" Width="120"/>


</Grid>
</Window>

接下来我该做什么?

最佳答案

实际上您还没有实现 INotifyPropertyChanged 接口(interface),因此您的属性更改通知不会执行。我对您的 Person 类进行了如下更改;

public class Person : IDataErrorInfo, INotifyPropertyChanged
{
private string _fname;
private string _lname;
public String Fname
{
get { return _fname; }
set { _fname = value; OnPropertyChanged("Fname"); }
}

public String Lname
{
get { return _lname; }
set { _lname = value; OnPropertyChanged("Lname"); }
}
public string Error
{
get { return ""; }
}

public string this[string columnName]
{
get
{
string result = null;
if (columnName == "Fname")
{
if (string.IsNullOrEmpty(Fname))
{
result = "First name is required.";
return result;
}
string st = @"!|@|#|\$|%|\?|\>|\<|\*";
if (Regex.IsMatch(Fname, st))
{
result = "Contains invalid characters.";
return result;
}
}
if (columnName == "Lname")
{
if (string.IsNullOrEmpty(Lname))
{
result = "Cannot be empty.";
return result;
}
}
return null;
}
}

#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged(String param)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(param));
}
}
#endregion
}

在MainWindow.cs 类中,只需将DataContext 设置为Person 类;

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new Person();
}
}

关于c# - WPF IDataErrorInfo 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34539663/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com