- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的 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 模型,而不仅仅是在控件失去焦点时。
第二个 TextBox
的 Background
属性绑定(bind)到 View 模型上名为 BackgroundColor
的 SolidColorBrush
属性.
在 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/
当检测鼠标x和y坐标时,最好像这样使用event.clientX和event.clientY: function show_coords(event){ var x=event.clientX;
我有以下代码: document.oncontextmenu = function(evt) { evt = evt || window.event; console.log(evt.
对于另一个问题,我遇到了一个似乎偶尔出现在 SO 的误解。一些提问者似乎认为触发器之于数据库就像事件之于 OOP 一样。 有没有人有一个很好的类比来解释为什么这是一个有缺陷的比较,以及误用它的后果?
$('body').keypress(function(event){ if(event.keyCode == 46){console.log('Delete Key Pressed')};
我正在制作一个“流体”文本区域,它根据内容调整其高度。我实际上正在尝试实现 this脚本。我有以下代码:https://ellie-app.com/Vjtvm6yrKWa1/4 问题是,当增加高度时,
我使用 Raphael .mouseover() 和 .mouseout() 事件来突出显示 SVG 中的某些元素。这工作正常,但在我单击一个元素后,我希望它停止突出显示。 在 Raphael doc
我目前正在开发一个应用程序,允许人们为在线广播电台安排“节目”。 我希望用户能够设置重复事件,例如:- “躁狂星期一”节目 - 每周一 9 点至 11 点“月中疯狂” - 每个月的第二个星期四“本月新
我有以下三个表格(简化版本): 已加载关卡: id(整数、主键、自动增量) globalId(整数,键) 日期(日期时间、键) serverId(Int,键) gamemodeId(Int,Key)
在我阅读 Gevent Tutorial 之后,我有一个关于 gevent.event.Event 的问题。 Event.set() 是否会唤醒所有被 Event.wait() 阻塞的函数? 就像下面
我对 cakephp ver3.1.3 没有经验 我按照说明实现了登录认证功能; http://book.cakephp.org/3.0/en/tutorials-and-examples/blog-
现在,我发送 10 个事件,每个事件有 1 个属性。但是当我想过滤特定事件并按属性选择事件时,在“事件属性”过滤器中仅显示前 7 个事件,而我为其余事件添加的事件仅显示“第一次”过滤器,为什么? 最佳
我不知道我的 Firefox 发生了什么! 我的aspx和javascript代码是这样的: function a() { alert(
中有3个事件fns重装 ,我可以对两者做同样的事情 reg-event-db和 reg-event-fx . reg-event-db之间的主要区别是什么, reg-event-fx和 reg-eve
我遇到了 Firefox keydown 行为,因为在没有聚焦于特定字段的情况下按下 Enter 键(实际上是任何键)不会触发 keydown 事件只会触发`按键事件。 这可能会非常令人困惑,因为 k
这是我的代码片段 public class Notation : INotifyPropertyChanged { public event PropertyChangedEventHandl
我可以在一个 Jsf2 xhtml 文件中有多个标签吗? 在那种情况下,关联的监听器将以什么顺序被调用? Mojarra 2.1.1/Apache Tomcat 7.0.22/PrimeFaces 3
我可以在一个 Jsf2 xhtml 文件中有多个标签吗? 在那种情况下,关联的监听器将以什么顺序被调用? Mojarra 2.1.1/Apache Tomcat 7.0.22/PrimeFaces 3
我有以下 JavaScript: $('#ge-display').click(function (event) { window.open('/googleearth/ge-display.ph
我需要确定触发事件的元素。 使用 event.target 获取相应的元素。 我可以从那里使用哪些属性? 引用 编号 节点名 我找不到关于它的大量信息,即使在 jQuery 上也是如此页,所以希望有人
我在pyGame中创建了一个Asteroidz克隆,并在pygame.vent.get()循环中有两个for Event,一个用于检查退出请求,以及游戏是否应该通过按空格键开始,然后在游戏中进一步尝试
我是一名优秀的程序员,十分优秀!