我有一个WPF应用程序,我必须检查TextBox
值和ComboBox
。如果它为空或不符合格式,则按钮单击事件应触发错误,如果ComboBox
中所选索引再次为0,则应触发错误。(如错误提供程序中)。
我在互联网上做了很多关于 IDataErrorInfo 解决方案的研究。但问题是如何在按钮单击事件上执行此操作。所有示例都是在表单加载上进行的。
我对WPF很新。以下是我的代码
public class ClientMap : IDataErrorInfo
{
public string CDSNo { get; set; }
public ClientMap(int ID)
{
Id = ID;
}
public ClientMap()
{
}
public string Error
{
get { throw new NotImplementedException(); }
}
public string this[string columnName]
{
get
{
string result = null;
if (columnName == "CDSNo")
{
if (string.IsNullOrEmpty(CDSNo))
result = "Please enter a CDS No";
else
{
string regEx = "[A-Z]{3}-\\d{9}-[A-Z]{2}-\\d{2}";
if (!Regex.IsMatch(CDSNo, regEx))
{
result = "Invalid CDS No";
}
}
}
return result;
}
}
public int Id { get; set; }
public CE.Data.Customer Customer { get; set; }
public CE.Data.Institute Institute { get; set; }
public bool Archived { get; set; }
public DateTime DateCreated { get; set; }
}
和XAML是
<Window.Resources>
<validation:ClientMap x:Key="data"/>
</Window.Resources>
<control:AutoCompleteTextBox Style="{StaticResource textBoxInError}">
<TextBox.Text>
<Binding Path="CDSNo" Source="{StaticResource data}"
ValidatesOnDataErrors="True"
UpdateSourceTrigger="Explicit">
<Binding.ValidationRules>
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</control:AutoCompleteTextBox>
请帮帮我。 感谢
答案 0 :(得分:9)
这是此article的修改代码。您需要从该站点的下载中获取参考和其他类。
<强> Window1.xaml 强>
<Window x:Class="SOTCBindingValidation.Window1" x:Name="This"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SOTCBindingValidation"
Title="SOTC Validation Test" Height="184" Width="390">
<Window.Resources>
<local:ErrorsToMessageConverter x:Key="eToMConverter" />
</Window.Resources>
<StackPanel Margin="5">
<TextBlock Margin="2">Enter An IPv4 Address:</TextBlock>
<TextBox x:Name="AddressBox">
<TextBox.Text>
<Binding ElementName="This" Path="IPAddress"
UpdateSourceTrigger="Explicit">
<Binding.ValidationRules>
<local:IPv4ValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<TextBlock Margin="2" Foreground="Red" FontWeight="Bold"
Text="{Binding ElementName=AddressBox,
Path=(Validation.Errors),
Converter={StaticResource eToMConverter}}" />
<Button Name="Btn1" Height ="30" Width="70" Click="Btn1_Click"></Button>
</StackPanel>
</Window>
<强> Window1.xaml.cs 强>
using System.Windows;
using System.Windows.Controls;
namespace SOTCBindingValidation
{
public partial class Window1 : Window
{
public static readonly DependencyProperty IPAddressProperty =
DependencyProperty.Register("IPAddress", typeof(string),
typeof(Window1), new UIPropertyMetadata(""));
public string IPAddress
{
get { return (string)GetValue(IPAddressProperty); }
set { SetValue(IPAddressProperty, value); }
}
public Window1()
{ InitializeComponent(); }
private void Btn1_Click(object sender, RoutedEventArgs e)
{
ForceValidation();
if (!Validation.GetHasError(AddressBox))
{
// Put the code you want to execute if the validation succeeds here
}
}
private void ForceValidation()
{
AddressBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
}
}