我有一个带有Master-Detail视图的项目,其中Master部分由一个选择对象的列表组成,Detail部分显示该对象的细节并允许编辑它。
我的问题是我不能允许2个对象具有相同的名称,而这听起来像一个简单的任务,结果证明我知道的验证过程并不能很好地发挥作用。
这是一个简短的例子。
XAML
<Window x:Class="WpfApplication1.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:WpfApplication1="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<ListView Grid.Column="0" ItemsSource="{Binding Path=People, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" Name="masterList"
DisplayMemberPath="Name" SelectedItem="{Binding Path=SelectedPerson}" />
<StackPanel Grid.Column="1" Margin="5" DataContext="{Binding Path=SelectedPerson}">
<TextBlock>Name</TextBlock>
<TextBox>
<TextBox.Text>
<Binding Path="Name" Mode="TwoWay" ValidatesOnDataErrors="True" NotifyOnValidationError="True">
</Binding>
</TextBox.Text>
</TextBox>
<TextBlock>Address</TextBlock>
<TextBox Text="{Binding Path=Address}" />
<TextBlock>Phone</TextBlock>
<TextBox Text="{Binding Path=Phone}" />
</StackPanel>
</Grid>
</Window>
人员类
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
public class Person {
public string Name { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
}
注册人类
public class RegisteredPeople {
public ObservableCollection<Person> People { get; set; }
public Person SelectedPerson { get; set; }
public RegisteredPeople() {
People = new ObservableCollection<Person>() {
new Person() {Name = "Ramzi", Address = "A1", Phone = "1"},
new Person() {Name = "Frank", Address = "A2", Phone = "12"},
new Person() {Name = "Ihab", Address = "A3", Phone = "123"}
};
}
}
这没有任何验证,但它显示了我想要的基本机制。
我尝试在两个类上实现IDataErrorInfo
,但没有取得任何成功:
Person类的其他实现
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
public class Person : System.ComponentModel.IDataErrorInfo, INotifyPropertyChanged {
private string m_name;
public string Name {
get { return m_name; }
set {
m_name = value;
OnPropertyChanged();
OnPropertyChanged("People"); //Name of the list that the master list is bound to.
}
}
public string Address { get; set; }
public string Phone { get; set; }
public string this[string columnName] {
get {
switch (columnName) {
case "Name":
if (string.IsNullOrEmpty(Name)) {
/** This one works, but from here I cannot compare with names of other Person objects. **/
return "The name cannot be empty.";
}
break;
default:
return string.Empty;
}
return String.Empty;
}
}
public string Error { get; }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
RegisteredPeople类的其他实现
public class RegisteredPeople : System.ComponentModel.IDataErrorInfo {
public ObservableCollection<Person> People { get; set; }
public Person SelectedPerson { get; set; }
public RegisteredPeople() {
People = new ObservableCollection<Person>() {
new Person() {Name = "Ramzi", Address = "A1", Phone = "1"},
new Person() {Name = "Frank", Address = "A2", Phone = "12"},
new Person() {Name = "Ihab", Address = "A3", Phone = "123"}
};
}
public string this[string columnName] {
get {
switch (columnName) {
case "People":
foreach (Person person1 in People) {
foreach (Person person2 in People) {
if (person1 == person2) {
continue;
}
if (person1.Name == person2.Name) {
return "Error, 2 people cannot have the same name.";
}
}
}
break;
default:
return string.Empty;
}
return string.Empty;
}
}
public string Error { get; }
}
我也尝试过使用ValidationRule
界面而没有任何成功。
以下是我的尝试:
XAML
我用名称替换了文本框:
<TextBox>
<TextBox.Text>
<Binding Path="Name" Mode="TwoWay" ValidatesOnDataErrors="True" NotifyOnValidationError="True">
<Binding.ValidationRules>
<WpfApplication1:EventNameValidationRule EventList="{Binding ElementName=masterList, Path=ItemsSource}" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
EventNameValidationRule
using System.Collections.ObjectModel;
using System.Globalization;
using System.Windows.Controls;
class EventNameValidationRule : ValidationRule {
public ObservableCollection<Person> EventList { get; set; }
public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
return new ValidationResult(false, "Duplicate names are not allowd.");
}
}
事情就是抛出这样的异常,说对EventList
的绑定并不好。 (如果没有这个列表,我显然没有要比较的起点。)
我的问题:如果有两个名字叫同名,我怎么能将名称标记为无效?
答案 0 :(得分:3)
您需要在Person
类中实现验证逻辑,即实现DataContext
接口的实际IDataErrorInfo
。
例如,您可以在RegisteredPeople
类中创建一个方法,该方法可以从Person
类的索引器中调用,.e.g。:
public class Person : IDataErrorInfo, INotifyPropertyChanged
{
private readonly IValidator _validator;
public Person(IValidator validator)
{
_validator = validator;
}
private string m_name;
public string Name
{
get { return m_name; }
set
{
m_name = value;
OnPropertyChanged();
}
}
public string Address { get; set; }
public string Phone { get; set; }
public string this[string columnName]
{
get
{
switch (columnName)
{
case "Name":
if (string.IsNullOrEmpty(Name))
{
/** This one works, but from here I cannot compare with names of other Person objects. **/
return "The name cannot be empty.";
}
else
{
return _validator.Validate(this);
}
default:
return string.Empty;
}
}
}
public string Error { get; }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public interface IValidator
{
string Validate(Person person);
}
public class RegisteredPeople : IValidator
{
public ObservableCollection<Person> People { get; set; }
public Person SelectedPerson { get; set; }
public string Validate(Person person)
{
if (person != null && People.Any(x => x != person && x.Name == person.Name))
return "Same name!";
return null;
}
public RegisteredPeople()
{
People = new ObservableCollection<Person>() {
new Person(this) {Name = "Ramzi", Address = "A1", Phone = "1"},
new Person(this) {Name = "Frank", Address = "A2", Phone = "12"},
new Person(this) {Name = "Ihab", Address = "A3", Phone = "123"}
};
}
}