我是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>