Я пытаюсь научиться реализовывать проверку данных, но моя первая попытка не запускает событие lblSource_Error; кто-нибудь знает, что я пропустил? XAMLМой первый C# WPF ValidationRule не стреляет
моего окна: Код
<Window x:Class="cCompleteWPFResourcesExamples.wValidationRule"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:cCompleteWPFResourcesExamples"
Title="wValidationRule" Height="300" Width="300">
<Window.Resources>
<local:Customer x:Key="rCustomer" Forename="InXaml" Surname="Created" ID="1"
AmountOutstanding="0"/>
</Window.Resources>
<StackPanel x:Name="stkMain" DataContext="{StaticResource rCustomer}">
<Label x:Name="lblSource" Validation.Error="lblSource_Error">
<Label.Content>
<Binding Path="ID" NotifyOnValidationError="True">
<Binding.ValidationRules>
<local:cIDValidationRule/>
</Binding.ValidationRules>
</Binding>
</Label.Content>
</Label>
<Label x:Name="lblErrorMessage" Content="No Error Yet"/>
</StackPanel>
</Window>
моего окна:
namespace cCompleteWPFResourcesExamples
{
/// <summary>
/// Interaction logic for wValidationRule.xaml
/// </summary>
public partial class wValidationRule : Window
{
Customer cus = new Customer();
public wValidationRule()
{
InitializeComponent();
cus.ID = 0;
stkMain.DataContext = cus;
}
private void lblSource_Error(object sender, ValidationErrorEventArgs e)
{
lblErrorMessage.Content = e.Error.ErrorContent.ToString();
}
}
}
Мой ValidationRule:
using System.Windows.Controls;
namespace cCompleteWPFResourcesExamples
{
public class cIDValidationRule : ValidationRule
{
public override ValidationResult Validate(object value,
System.Globalization.CultureInfo cultureInfo)
{
int iValue = (int)value;
if (iValue == 0) return new ValidationResult(false, "No ID number");
return new ValidationResult(true, null);
}
}
}
Объект клиента очень прост: всего несколько свойств.
Спасибо!
Джеймс
Этикетка? Ярлыки не являются входами, поэтому я не думаю, что для них будет работать проверка ... – Will
Поле id вашего пользовательского объекта можно отредактировать на форме или просто показать? Правило проверки будет проверено при изменении значения –