2

I have 3 text boxes and a search button.

<TextBox Grid.Row="1" Grid.ColumnSpan="2" VerticalAlignment="Center" HorizontalAlignment="Stretch" Text="{Binding Id, Mode=TwoWay}"/>
<TextBox Grid.Row="3" Grid.ColumnSpan="2" VerticalAlignment="Center" HorizontalAlignment="Stretch" Text="{Binding Name, Mode=TwoWay}"/>
<TextBox Grid.Row="5" Grid.ColumnSpan="2" VerticalAlignment="Center" HorizontalAlignment="Stretch" Text="{Binding Phone, Mode=TwoWay}"/>
<Button Grid.Row="8" Grid.ColumnSpan="2" Content="Search" Command="{Binding SearchPerson}" />

I have implemented logic where the user types something in any of the textboxes, then leaves the text box, at which point the search button gets enabled.

But, I want the search button to be enabled when a user "starts typing" (the string should not be null or whitespace) in any of the text boxes.

How do i implement this??

Many thanks in advance...

4

3 回答 3

3

如果您希望在键入时更新源,则需要将绑定上的UpdateSourceTrigger属性设置为。PropertyChanged

因此,将您的绑定更改为:

Text="{Binding Id, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Text="{Binding Phone, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
于 2013-07-01T14:38:54.697 回答
1

好的,只需将我的评论转换为答案。

MSDN Docs中所述

TextBox.Text 属性的默认 UpdateSourceTrigger 值为 LostFocus。

因此,要使其“尽快”,您只需要显式打开UpdateSourceTrigger=PropertyChanged绑定TextBox.Text即可。

像(在每个TextBox):

Text="{Binding Id, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
于 2013-07-01T14:53:26.000 回答
0

为什么不使用简单的 TextBox->OnTextChange 事件:

button1.IsEnabled = !string.IsNullOrWhiteSpace((sender as TextBox).Text);
于 2013-07-01T14:52:38.193 回答