c# - WPF:设置属性不会在文本框值更改时触发

标签 c# wpf xaml mvvm

我有一个绑定(bind)用户模型的 EditUserViewModel。在用户模型中,我有公共(public)属性,例如名字、姓氏、年龄等。

编辑用户 View 模型

public class EditUserViewModel : BindableBase
{
    private User _user;

    public EditUserViewModel(User user)
    {
        SaveCommand = new DelegateCommand(EditUser, CanSaveCommand);
        CancelCommand = new DelegateCommand(Cancel);
        User = user;
    }

    public User User
    {
        get{ return _user; }
        set
        {
            _user = value;
            OnPropertyChanged(() => User);
            SaveCommand.RaiseCanExecuteChanged();
        }
    }

    public DelegateCommand SaveCommand { get; set; }
    public ICommand CancelCommand { get; set; }

    public Func<bool> CanSaveCommand { get { return CanEditUser; } }

    private void EditUser()
    {
        Confirmed = true;
        FinishInteraction();
    }

    private bool CanEditUser()
    {
        if (string.IsNullOrEmpty(_user.Firstname) || _user.Firstname.Length < 3) return false;
        if (string.IsNullOrEmpty(_user.Lastname) || _user.Lastname.Length < 3) return false;

        return true;
    }
}

编辑用户查看
<UserControl x:Class="UserModule.Views.EditUserView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>

        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <TextBlock FontWeight="Bold" Grid.Row="0" Grid.Column="0">First name:</TextBlock>
        <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=User.Firstname, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

        <TextBlock FontWeight="Bold" Grid.Row="1" Grid.Column="0">First name:</TextBlock>
        <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Path=User.Lastname, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

        <Button x:Name="OkButton" Grid.Row="2" Grid.Column="0" Command="{Binding SaveCommand}" Content="OK"/>
        <Button x:Name="CancelButton" Grid.Row="2" Grid.Column="1" Command="{Binding CancelCommand}" Content="Cancel"/>
    </Grid>

</UserControl>

用户
public User 
{
    public string Firstname  { get; set; }
    public string Lastname  { get; set; }
    public string Age  { get; set; }
}

但是,当我更改名字或姓氏时,User 属性不会触发其 setter 。但是,如果我没有使用 User 类属性,而是在 Viewmodel 中有 Firstname 和 Lastname 字符串属性,那么当我在 GUI 中更改它们的值时,它们的 setter 会被触发。我需要做任何特定的事情来使这个类属性绑定(bind)工作吗?

最佳答案

确保用户类实现 INotifyPropertyChanged

  public class User : INotifyPropertyChanged
{
    private String _firstname;
    public String Firstname
    {
        get
        {
            return _firstname;
        }

        set
        {
            if (_firstname == value)
            {
                return;
            }

            _firstname = value;
            OnPropertyChanged();
        }
    } 
    private String _lastName;
    public String LastName
    {
        get
        {
            return _lastName;
        }

        set
        {
            if (_lastName == value)
            {
                return;
            }

            _lastName = value;
            OnPropertyChanged();
        }
    }
    public ObservableCollection<DataGridEntry> DataGridEntries { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

关于c# - WPF:设置属性不会在文本框值更改时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28027517/

相关文章:

c# - IsTabStop ="False"对我的 WPF 应用程序没有影响

c# - System.Windows.Baml2006.TypeConverterMarkupExtension' 抛出异常 .' Line number ' 6' and line position ' 10

c# - Visual Studio WPF 设计器不使用覆盖的 SystemColors

xaml - Xamarin.Forms:ContentPage 中不可见 ToolbarItems

c# - 在后面的代码中从 DataTable 设置 javascript 数组

c# - 如何在autofac中的解析时间上传递参数

c# - 顶级通用类库的 Class.Class vs Namespace.Class?

c# - WPF .GetElementById() 方法

c# - 通过 C# 的动态 XAML

c# - 如何制作这样的数组