C# 如何使用MVVM通过命令正确实现TextChanged事件

C# 如何使用MVVM通过命令正确实现TextChanged事件,c#,wpf,mvvm,icommand,C#,Wpf,Mvvm,Icommand,我正在学习使用MVVM模式的WPF。我的应用程序是计算体重指数,所以它非常简单——只是为了帮助我理解这个模式的基础 我做了一点实验,决定通过命令实现TextChanged事件,让用户在输入身高或体重时看到整体BMI标签的变化 我使用TextChanged命令的文本框以双向模式绑定到ViewModel属性,因此我认为如果在发生TextChanged事件时对绑定到这些文本框的属性引发INotifyPropertyChanged事件,它将自动更新视图,但不会 所以问题是,我做错了什么,我如何才能正确地

我正在学习使用MVVM模式的WPF。我的应用程序是计算体重指数,所以它非常简单——只是为了帮助我理解这个模式的基础

我做了一点实验,决定通过命令实现TextChanged事件,让用户在输入身高或体重时看到整体BMI标签的变化

我使用TextChanged命令的文本框以双向模式绑定到ViewModel属性,因此我认为如果在发生TextChanged事件时对绑定到这些文本框的属性引发INotifyPropertyChanged事件,它将自动更新视图,但不会

所以问题是,我做错了什么,我如何才能正确地实施它

另外,除了视图更新之外,其他一切都在工作(使用了命令,我用断点检查了一下,它只是不改变视图)

提前谢谢

自定义命令类:

public class CustomCommand : ICommand
{

    Action<object> action;
    Predicate<object> predicate;

    public CustomCommand(Action<object> execute, Predicate<object> canExecute)
    {
        action = execute;
        predicate = canExecute;
    }

    public event EventHandler CanExecuteChanged
    {
        add
        {
            CommandManager.RequerySuggested += value;
        }
        remove
        {
            CommandManager.RequerySuggested -= value;
        }
    }

    public bool CanExecute(object parameter)
    {
        if (predicate(parameter))
            return true;
        else
            return false;
    }

    public void Execute(object parameter)
    {
        action(parameter);
    }
}
公共类CustomCommand:ICommand
{
行动;
谓词;
公共自定义命令(操作执行,谓词canExecute)
{
行动=执行;
谓词=canExecute;
}
公共事件事件处理程序CanExecuteChanged
{
添加
{
CommandManager.RequerySuggested+=值;
}
去除
{
CommandManager.RequerySuggested-=值;
}
}
公共布尔CanExecute(对象参数)
{
if(谓词(参数))
返回true;
其他的
返回false;
}
public void Execute(对象参数)
{
作用(参数);
}
}
两个文本框之一:

<TextBox HorizontalAlignment="Left" Height="23" Margin="148,83,0,0" TextWrapping="Wrap" Text="{Binding Person.Weight, Mode=TwoWay}" VerticalAlignment="Top" Width="76">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="TextChanged">
                <i:InvokeCommandAction Command="{Binding Path=textChangedCommand}"></i:InvokeCommandAction>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBox>

和ViewModel,其中TextChanged方法被传递给命令

public class MainWindowViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler propertyChanged = PropertyChanged;

        if (propertyChanged != null)
            propertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }


    public ICommand textChangedCommand { get; set; }

    //public List<float> BMI_Changed;

    private PersonInfo person;


    public PersonInfo Person
    {
        get
        {
            return person;
        }
        set
        {
            person = value;
            OnPropertyChanged("Person");
        }
    }

    public MainWindowViewModel()
    {
        //BMI_Changed = new List<float>();
        textChangedCommand = new CustomCommand(TextChanged, CanBeChanged);
        person = Data.personInfo;
    }


    private void TextChanged(object obj)
    {
        OnPropertyChanged("BMI");
        OnPropertyChanged("Weight");
        OnPropertyChanged("Height");
    }

    private bool CanBeChanged(object obj)
    {
        return true;
    }
}
public类MainWindowViewModel:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
公共void OnPropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler propertyChanged=propertyChanged;
if(propertyChanged!=null)
propertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
公共ICommand textChangedCommand{get;set;}
//公共列表BMI_已更改;
私人信息人;
公众人物信息
{
得到
{
返回人;
}
设置
{
人=价值;
不动产变更(“人”);
}
}
公共主窗口视图模型()
{
//BMI_Changed=新列表();
textChangedCommand=newcustomcommand(TextChanged,CanBeChanged);
person=Data.personInfo;
}
私有无效文本已更改(对象obj)
{
不动产变更(“BMI”);
不动产变更(“重量”);
不动产变更(“高度”);
}
私有布尔值可以更改(对象对象对象)
{
返回true;
}
}
我的视图代码的其余部分,用于概述:

<Window x:Class="SportCalculators_MVVM.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:local="clr-namespace:SportCalculators_MVVM"
    xmlns:enum="clr-namespace:SportCalculators_MVVM.Model"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    mc:Ignorable="d"
    Title="MainWindow" Height="340.278" Width="260.256" Loaded="Window_Loaded"
    DataContext="{Binding Source={StaticResource viewModelLocator}, Path=mainWindowViewModel}">

<Grid x:Name="grid">


    <Slider x:Name="mass" HorizontalAlignment="Right" Margin="0,128,58,0" VerticalAlignment="Top" Width="155" Value="{Binding Person.Weight, Mode=TwoWay}" Maximum="150" Minimum="20"/>
    <Slider x:Name="height" HorizontalAlignment="Left" Margin="40,210,0,0" VerticalAlignment="Top" Width="155" Minimum="100" Maximum="230" Value="{Binding Person.Height, Mode=TwoWay}"/>
    <RadioButton x:Name="sex" Content="Kobieta" HorizontalAlignment="Left" Margin="45,41,0,0" VerticalAlignment="Top" IsChecked="{Binding Person.Sex, Converter={StaticResource ResourceKey=genderConverter}, ConverterParameter={x:Static enum:Sex.Female}}"/>
    <RadioButton x:Name="sex1" Content="Mężczyzna" HorizontalAlignment="Left" Margin="150,41,0,0" VerticalAlignment="Top" IsChecked="{Binding Person.Sex, Converter={StaticResource ResourceKey=genderConverter}, ConverterParameter={x:Static enum:Sex.Male}}"/>
    <Label x:Name="massLabel" Content="Waga" HorizontalAlignment="Left" Margin="40,80,0,0" VerticalAlignment="Top"/>
    <Label x:Name="heightLabel" Content="Wzrost" HorizontalAlignment="Left" Margin="39,167,0,0" VerticalAlignment="Top"/>
    <Label x:Name="label" Content="{Binding Person.BMI}" HorizontalAlignment="Left" Margin="39,274,0,0" VerticalAlignment="Top"/>
    <Button Content="Statystyki" HorizontalAlignment="Left" Margin="149,274,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.325,-0.438"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="148,83,0,0" TextWrapping="Wrap" Text="{Binding Person.Weight, Mode=TwoWay}" VerticalAlignment="Top" Width="76">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="TextChanged">
                <i:InvokeCommandAction Command="{Binding Path=textChangedCommand}"></i:InvokeCommandAction>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBox>

    <TextBox HorizontalAlignment="Left" Height="23" Margin="148,170,0,0" TextWrapping="Wrap" Text="{Binding Person.Height, Mode=TwoWay}" VerticalAlignment="Top" Width="76">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="TextChanged">
                <i:InvokeCommandAction Command="{Binding Path=textChangedCommand}"></i:InvokeCommandAction>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBox>



</Grid>

Ed Plunkett给出了最简单的解决方案:

当TextChanged发生时,无需编写全部代码来实现该命令,有一个绑定属性UpdateSourceTrigger可确定何时应该进行更新,默认情况下,它设置为LostFocus,例如,当您单击另一个控件时,如果您想在用户键入时更新它,您需要将值设置为PropertyChanged,仅此而已

<TextBox Text="{Binding Person.Weight, UpdateSourceTrigger=PropertyChanged}">


只需在文本框绑定到的属性的属性设置器中执行此操作。您可以忽略双向标志,因为这是该属性的默认值,但您应该添加UpdateSourceTrigger=PropertyChanged,这样每当用户键入字符时它都会更新该属性。非常感谢,它完全按照我所希望的方式工作:)对于任何有类似问题的人,我都会写一个答案。哦,我的上帝,我已经找了好几个星期了。你太牛了非常感谢。