- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果三个条目中的任何一个为空,我想禁用发送按钮,但如何以 MVVM 方式实现?
我想到了 CanExecute 委托(delegate),但如何在 TextChanged 触发时触发它?
此外,如果我选择这些行为,如果我正在使用 Behavior<Entry>
,我该如何与按钮等其他控件进行通信?
这是 View :
<ContentPage.Content>
<AbsoluteLayout>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="56"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="10"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Label Text="Contact Us" FontSize="Medium" Grid.ColumnSpan="3"/>
<Entry Text="{Binding ContactData.message_name}" x:Name="subject" Grid.Row="2" Grid.Column="1" Placeholder="Subject"/>
<Entry Keyboard="Email" Text="{Binding ContactData.receiver_email}" x:Name="email" Grid.Row="3" Grid.Column="1" Placeholder="Email"/>
<Editor Text="{Binding ContactData.message_subject}" x:Name="body" Grid.Row="4" Grid.Column="1" />
<Button Grid.Row="5" Grid.Column="1" Command="{Binding ContactFormSent}" Text="Send"/>
</Grid>
</AbsoluteLayout>
</ContentPage.Content>
在 View 模型中:
public ContactViewModel()
{
ContactFormSent = new RelayCommand(SendContactInfo);
ContactData = new ContactModel();
}
private bool CanSend() //this only get called when the view model is constructed
{
return !(string.IsNullOrWhiteSpace(ContactData.receiver_email) && string.IsNullOrWhiteSpace(ContactData.message_subject) &&
string.IsNullOrWhiteSpace(ContactData.message_name));
}
在“行为”选项中,我希望它与 Entry
一起使用和 Editor
,所以我要走的路是Behavior
类,而不是通用版本?如果是这样,我该如何实现?
最佳答案
通常,为了使用 MVVM 模式对 View 事件使用react,建议您使用 EventToCommandBehaviour .
但在这种情况下不需要,因为绑定(bind)到 Entry
的属性的 setter 应该在每次文本更改时触发。您可以使用它在命令上触发 ChangeCanExecute()
以通知 View 现在可以启用/禁用按钮。
例如:
string _firstName;
public string FirstName
{
get { return _firstName; }
set
{
if (_firstName != value)
SendCommand.ChangeCanExecute();
SetProperty(ref _firstName, value, nameof(FirstName));
}
}
string _lastName;
public string LastName
{
get { return _lastName; }
set
{
if (_lastName != value)
SendCommand.ChangeCanExecute();
SetProperty(ref _lastName, value, nameof(LastName));
}
}
string _email;
public string Email
{
get { return _email; }
set
{
if (_email != value)
SendCommand.ChangeCanExecute();
SetProperty(ref _email, value, nameof(Email));
}
}
Command _sendCommand;
public Command SendCommand
{
get
{
return _sendCommand ?? (_sendCommand = new Command(OnSend, CanSend));
}
}
bool CanSend(object obj)
{
return Validate();
}
void OnSend(object obj)
{
if(Validate())
{
//actual button click execution
}
}
bool Validate()
{
// disable button if any entry empty
return !string.IsNullOrEmpty(_firstName)
&& !string.IsNullOrEmpty(_lastName)
&& !string.IsNullOrEmpty(_email);
}
关于c# - 将 Entry TextChanged 绑定(bind)到 Xamarin.Forms 中按钮命令的 CanExecute 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47131362/
最近有人问我是否可以在 Excel VBA 中制作一个宏,允许用户输入两个数字并让它自动放到下一行。这样做的目的是让他们可以一次输入两个数字的测试成绩,而无需按回车键,因为他们不擅长打字。 当我第一次
我在 Windows Phone 上的应用程序中有三个 TextBox。我想在用户在其中任何一个中插入输入后立即更改这三个 TextBox 的数值。 我的 xaml 看起来像这样: 我在 C#
我有 CalendarExtender 并将其绑定(bind)到文本框,但是当我在 textchanged 事件中编写代码时发现它没有触发;这里是我的代码片段: 代码隐藏: protected vo
我在做一个项目。我有文本框,通过 TextChanged 事件我将文本更改为大写 txt.ToUpper(); 然后 selectionStart 从头开始。我将其更改为 textbox.text
我仍然是 Android 开发的新手,我想做的是创建一个监听器,它将包含两个 TextView 对象,它们分别保存面积和周长。宽度和高度是 EditText 对象。输入宽度和高度后,应根据 calcA
我有一个文本框,它有一个相当庞大的 _TextChanged 事件处理程序。在正常打字条件下,性能还可以,但当用户执行长时间的连续操作时,性能会明显滞后,例如按住退格键一次删除大量文本。 例如,事件用
在尝试做一些更复杂的事情时,我遇到了一种我不太了解的行为。 假设下面的代码处理 textChanged 事件。 private void textChanged(object sender, Tex
我正在关注 M-V-VM我的 WPF UI 的模式。我想将文本框的 TextChanged 事件的命令连接到我的 ViewModel 类中的命令。我能想到的完成此任务的唯一方法是从 TextBox 控
我有一个简单的 aspx 文件 这是 test4.aspx.vb 代码文件 Partial Class test4 Inhe
我正在使用绑定(bind)到对象的基本文本框。基本上,我想要做的是每次编辑框中的文本并且用户取消选择框或编辑底层绑定(bind)数据时调用一个方法。我现在使用的是 TextChanged 事件,但这有
我的 TabControl 页面中有一个 RichTextBox。请注意,RichTextBox 是使用以下代码以编程方式创建的: TabPage addedTabPage = new TabPage
这是我的第一个问题。我接受过非常基本的 Java 培训,现在正在 Android Studio 中开发一个简单的应用程序。该应用程序是一个温度转换器,它读取一个文本字段,并在应用一种方法后,它实时在另
我有一个 TextChanged我的事件 ComboBox喜欢; private void comboBox1_TextChanged(object sender, EventArgs e) {
我仍在学习 C#,如果这是一个愚蠢的问题,请原谅我,也请原谅我的长篇文章,因为我不确定如何说这个,而不是“长”的方式。 我的场景: 我正在使用 DockPanel Suite对于我的应用程序,我有一个
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我刚开始使用 C# 和 Visual Studio,但遇到了问题。 我正在尝试使用 TextChanged 事件计算三个不同乘法的总值,因此每次我在用于乘法的文本框中输入数字时,总文本框都会更新。 t
所以我想做的是能够连续输入所有 10 个文本框。我从第一个开始,然后在输入 3 个字符后将焦点移至下一个,并在输入 3 个字符后将焦点移至下一个。 我已经完成了那部分,但是如果第二个文本框中已经有文本
我想知道是否有可能制作KeyDown TextChanged 之前的事件触发? 我为一个简单的 TextBox 添加了这两个事件现在我想检查是否 Delete键被按下。如果是, , 如果没有 .我创
我目前正在处理一个项目,但出于某种原因,我的文本框的 textchanged 事件没有触发。我试图在我的代码中放置一个断点,但他没有到达那里,所以他的事件没有触发 imo。
我在 xml 中制作了一个不可见的按钮,我想在我的 EditText 中创建某个字符串值时再次使按钮可见。当使用 if 语句满足值时,我使用了 TextWatcher 检查。然而,当显示按钮的代码被执
我是一名优秀的程序员,十分优秀!