wpf-mvvm如何使用icommand验证数据?

我是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>
iCMS 回答:wpf-mvvm如何使用icommand验证数据?

使用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

输出:

enter image description here

此处是完整来源的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应该会有所帮助。

本文链接:https://www.f2er.com/1570085.html

大家都在问