gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:40:19 27 4
gpt4 key购买 nike

我正在学习使用 MVVM 模式的 WPF。我的应用正在计算 body 质量指数,所以它非常简单 - 只是为了帮助我理解这种模式的基础。

我进行了一些试验,并决定通过命令实现 TextChanged 事件,以允许用户在输入高度或体重时看到整体 BMI 标签的变化。

我在其中使用 TextChanged 命令的文本框在 TwoWay 模式下绑定(bind)到 ViewModel 属性,因此我认为如果在发生 TextChanged 事件时在绑定(bind)到这些文本框的属性上引发 INotifyPropertyChanged 事件,它将自动更新 View ,但它不会

所以问题是,我做错了什么,我该如何正确实现?

附言。除了 View update 之外的所有其他东西都在工作(使用命令,我用断点检查它只是不改变 View)

提前致谢

自定义命令类:

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);
}
}

两个文本框之一:

<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;
}
}

我的其余 View 代码,用于一般概述:

<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 发生时,无需编写一大堆代码来实现命令,有一个绑定(bind)属性 UpdateSourceTrigger 确定何时应该更新,默认设置为 LostFocus 例如,当您单击另一个控件时,如果您想在用户键入时更新它,您需要将值设置为 PropertyChanged 就这样!

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

关于c# - 如何使用 MVVM 通过命令正确实现 TextChanged 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44710145/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com