问题描述
我是WPF开发的新手,并且我对Google的了解也不多。我不知道如何使用ICommand
验证模型数据。我知道这里有注释,并且ICommand
接口提供了一种canExecute
方法。例如,我想说的是姓名和姓氏。
我在Model类中使用[required(ErrorMessage = "Title is required.")]
进行了尝试,但无法正常工作。也许有人可以帮助我。
public class Student : INotifyPropertyChanged
{
private string name;
private string surname;
private int age;
private string course;
public string Course
{
get { return course; }
set
{
course = value;
OnPropertyChanged();
}
}
public int Age
{
get { return age; }
set
{
age = value;
OnPropertyChanged();
}
}
public string Surname
{
get { return surname; }
set
{
surname = value;
OnPropertyChanged();
}
}
public string Name
{
get { return name; }
set
{
name = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(name));
}
}
public class Studentviewmodel
{
private IList<Student> _studentList;
public Studentviewmodel()
{
_studentList = new List<Student>
{
new Student{Name="Todd",Surname="Johnsen",Age=29,Course="Software-Development"},new Student{Name="Mike",Surname="Kroshka",Age=31,Course="Marketing"},new Student{Name="Marie",Surname="Tedd",Age=21,new Student{Name="Susane",Surname="Müller",new Student{Name="Herbert",Surname="Rehl",Age=18,new Student{Name="Markus",Surname="Stanz",Age=23,new Student{Name="Sabine",Surname="Bergsen",Age=19,Course="Marketing"}
};
}
public IList<Student> Students
{
get { return _studentList; }
set { _studentList = value; }
}
private ICommand mUpdater;
public ICommand UpdateCommand
{
get
{
if(mUpdater == null)
{
mUpdater = new Updater();
}
return mUpdater;
}
set
{
mUpdater = value;
}
}
}
public class Updater : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
}
}
<Window x:Class="Student_list_mvvm_wpf_core.MainWindowView"
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:Student_list_mvvm_wpf_core"
mc:Ignorable="d"
Title="Student-list" Height="350" Width="600">
<Grid>
<Grid.ColumnDeFinitions>
<ColumnDeFinition Width="auto"></ColumnDeFinition>
<ColumnDeFinition Width="*"></ColumnDeFinition>
<ColumnDeFinition Width="*"></ColumnDeFinition>
</Grid.ColumnDeFinitions>
<Grid.RowDeFinitions>
<RowDeFinition Height="auto"></RowDeFinition>
<RowDeFinition Height="auto"></RowDeFinition>
<RowDeFinition Height="auto"></RowDeFinition>
<RowDeFinition Height="auto"></RowDeFinition>
<RowDeFinition Height="*"></RowDeFinition>
</Grid.RowDeFinitions>
<Label Grid.Row="0">Name</Label>
<Label Grid.Row="1">Surname</Label>
<Label Grid.Row="2">Age</Label>
<Label Grid.Row="3">Course</Label>
<Button x:Name="btnUpdateStudent" Grid.Row="3" Grid.Column="2"
Width="100" Margin="2" HorizontalAlignment="Left"
Command="{Binding Path=UpdateCommand}">Update Student</Button>
<ListView x:Name="studentGrid" ItemsSource="{Binding Students}"
Grid.Row="4" Grid.ColumnSpan="3" Margin="5">
<ListView.View>
<GridView x:Name="gridStudent">
<GridViewColumn Header="Name" displayMemberBinding="{Binding Name}" Width="100" />
<GridViewColumn Header="Surname" displayMemberBinding="{Binding Surname}" Width="100" />
<GridViewColumn Header="Age" displayMemberBinding="{Binding Age}" Width="50" />
<GridViewColumn Header="Course" displayMemberBinding="{Binding Course}" Width="200" />
</GridView>
</ListView.View>
</ListView>
<TextBox Grid.Row="0" Grid.Column="1" Width="200"
HorizontalAlignment="Left" Margin="2"
Text="{Binding SelectedItem.Name,ElementName=studentGrid}"
x:Name="txtName"></TextBox>
<TextBox Grid.Row="1" Grid.Column="1" Width="200"
HorizontalAlignment="Left" Margin="2"
Text="{Binding SelectedItem.Surname,ElementName=studentGrid}"
x:Name="txtSurname"></TextBox>
<TextBox Grid.Row="2" Grid.Column="1" Width="200"
HorizontalAlignment="Left" Margin="2"
Text="{Binding SelectedItem.Age,ElementName=studentGrid}"
x:Name="txtAge"></TextBox>
<TextBox Grid.Row="3" Grid.Column="1" Width="200"
HorizontalAlignment="Left" Margin="2"
Text="{Binding SelectedItem.Course,ElementName=studentGrid}"
x:Name="txtCourse"></TextBox>
</Grid>
解决方法
使用IDataErrorInfo
验证模型看起来更适合您的情况。 首先,更新您的ViewModel
,使其属性绑定到ListView
的{{1}}:
SelectedItem
第二,更新 public Student SelectedStudent
{
get { return _selectedStudent; }
set { _selectedStudent = value; }
}
的模型以进行数据验证:
Student
最后,更新您的xaml:
-在您的public class Student : INotifyPropertyChanged,IDataErrorInfo
{
private string name;
private string surname;
private int age;
private string course;
public string Course
{
get { return course; }
set
{
course = value;
OnPropertyChanged();
}
}
public int Age
{
get { return age; }
set
{
age = value;
OnPropertyChanged();
}
}
public string Surname
{
get { return surname; }
set
{
surname = value;
OnPropertyChanged();
}
}
public string Name
{
get { return name; }
set
{
name = value;
OnPropertyChanged();
}
}
public string Error => string.Empty;
public string this[string columnName]
{
get
{
string result = null;
if (columnName == "Name")
{
if (string.IsNullOrEmpty(Name))
result = "Name is required.";
}
if (columnName == "Surname")
{
if (string.IsNullOrEmpty(Surname))
result = "Surname is required.";
}
return result;
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(name));
}
}
ListView's
和您创建的媒体资源之间绑定。
-验证SelectedItem
文本
TextBox's
-添加一个<TextBox Grid.Row="0" Grid.Column="1" Width="200"
HorizontalAlignment="Left" Margin="2"
Text="{Binding SelectedStudent.Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=true,NotifyOnValidationError=False}"
x:Name="txtName"></TextBox>
<TextBox Grid.Row="1" Grid.Column="1" Width="200"
HorizontalAlignment="Left" Margin="2"
Text="{Binding SelectedStudent.Surname,NotifyOnValidationError=False}"
x:Name="txtSurname"></TextBox>
以在满足适当条件时启用和禁用MultiDataTrigger
。
Button
输出:
此处是完整来源的Gist。如果您坚持使用<Button x:Name="btnUpdateStudent" Grid.Row="3" Grid.Column="2"
Width="100" Margin="2" HorizontalAlignment="Left"
Content="Update Student" Command="{Binding UpdateCommand}">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="IsEnabled" Value="False" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=txtName,Path=(Validation.HasError)}" Value="false" />
<Condition Binding="{Binding ElementName=txtSurname,Path=(Validation.HasError)}" Value="false" />
</MultiDataTrigger.Conditions>
<Setter Property="IsEnabled" Value="True" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
,this应该会有所帮助。