gpt4 book ai didi

c# - WPF MVVM : how to Update UI controllers based on Event

转载 作者:太空宇宙 更新时间:2023-11-03 12:17:35 25 4
gpt4 key购买 nike

我的 UserControl 中有 2 个 TextBox Controller ,我们称它们为 TextBox1 和 TextBox2。

在我的旧代码中,当触发 TextBox2 TextChanged 事件时,我更新了 TextBox1 背景。在 xaml.cs 中使用事件处理程序,这既简单又直接。

    private void textBox_TextChanged(object sender, TextChangedEventArgs e) {
// use controllers Names.
}

但是我读到这违反了 MVVM 标准。基本上不要在 xaml.cs 中添加额外的代码!

在寻找答案的过程中,我发现了 2 种我比较理解的方法:

1- 有些人建议我使用 PropertyChanged 来触发另一个事件。我注意到 PropertyChanged 事件不会触发,直到 TextBox 失去焦点。这不是我要找的。我希望 TextBox1 在用户向 TextBox2 输入内容后立即更新。但是,我仍然不确定在哪里告诉代码“如果 TextBox TextChanged,则更改 TextBox1 背景”。

2- 另一种方法是使用 Behaviors,这对我来说是全新的,我能够立即在 TextBox2 上触发事件 TextChanged,但我不知道如何访问 TextBox1 的属性!

我的问题:处理我在 MVVM 方法中寻找的要求的正确方法是什么?

最佳答案

您可以在 View 模型中执行所有这些逻辑。此特定示例使用 AgentOctal.WpfLib引发 PropertyChanged 通知的基础 ViewModel 类的 NuGet 包(免责声明:我是该包的作者),但您可以使用任何您想要的系统,只要它属性实现 INotifyPropertyChanged


在此示例中,您在第一个 TextBox 中输入的字母越多,第二个 TextBox 的背景就越蓝。

第一个 TextBox 将其 Text 属性绑定(bind)到 View 模型上的 Text 属性。绑定(bind)将 UpdateSourceTrigger 设置为 PropertyChanged,以便绑定(bind)在每次属性更改时更新 View 模型,而不仅仅是在控件失去焦点时。

第二个 TextBoxBackground 属性绑定(bind)到 View 模型上名为 BackgroundColorSolidColorBrush 属性.

在 View 模型中,TextBox 的 setter 包含确定第二个 TextBox 颜色的逻辑。

通过使用 Color 而不是 SolidColorBrush 和可以更改 IValueConverter 可能会更好地实现这一点>ColorBrush 中,但它应该作为一个不错的起点。

所有代码都在 View 模型中,代码隐藏是空的。


XAML:

<Window
x:Class="VmBindingExample.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:local="clr-namespace:VmBindingExample"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="525"
Height="350"
mc:Ignorable="d">
<Window.DataContext>
<local:MainWindowVm />
</Window.DataContext>
<StackPanel Margin="20" Orientation="Vertical">
<TextBox
Margin="4"
MaxLength="10"
Text="{Binding Path=Text, UpdateSourceTrigger=PropertyChanged}" />
<TextBox Margin="4" Background="{Binding BackgroundColor}">The color of this will reflect the length of the first textbox.</TextBox>
</StackPanel>
</Window>

View 模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using AgentOctal.WpfLib;

namespace VmBindingExample
{
using System.Windows.Media;

public class MainWindowVm : ViewModel
{
private string _text;

public string Text
{
get
{
return _text;
}

set
{
SetValue(ref _text, value);
byte red = (byte)(255 / 10 * (10 - _text.Length));
BackgroundColor = new SolidColorBrush(Color.FromArgb(255, red, 255, 255));
}
}

private Brush _backgroundColor;

public Brush BackgroundColor
{
get
{
return _backgroundColor;
}

set
{
SetValue(ref _backgroundColor, value);
}
}
}
}

关于c# - WPF MVVM : how to Update UI controllers based on Event,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49037677/

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