WPF中TextBox更改完了之后进行操作

WPF中TextBox的Text更改的相关方法有两种

  • TextChanged
  • SourceUpdated

TextChanged

事件

在 TextBox 控件中的文本发生更改时使用 TextChanged 事件执行方法,但是这个有个问题,只要更改了就会触发,比如我要输入“12”,输入“1”就触发一次,输入“2”又触发一次,一共触发2次。感觉好像不太对。

<TextBox TextChanged="TextBox_TextChanged"/>
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
	//TODO
}

Command

同样的,因为MVVM的思路,将它写成Command的方式,同样是更改了就会触发。因为其实它的内部也只是监听事件,当事件被触发的时候,执行Command。

<TextBox>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="TextChanged">
            <i:InvokeCommandAction Command="{Binding TextChangedCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>

SourceUpdated

首先要看TextBox的Text这个依赖属性本身,他的UpdateSourceTrigger值是LostFocus。

TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(TextBox), 
						new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal, 
						OnTextPropertyChanged, CoerceText, isAnimationProhibited: true, UpdateSourceTrigger.LostFocus));

如果你去看UpdateSourceTrigger 枚举,他是这么说的:大多数依赖属性的默认值为 PropertyChanged,而 Text 属性的默认值为 LostFocus,它实际上是丢失焦点之后才调用OnTextPropertyChanged。

最终将Text绑定增加了一个属性NotifyOnSourceUpdated=True,然后增加一个TextChangedCommand,如下所示,其中SourceUpdated很关键,他能在TextBox的Text丢失焦点,也就是当数据写完了,要更新源的时候触发TextChangedCommand

<TextBox Text="{Binding MyText, NotifyOnSourceUpdated=True}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SourceUpdated">
            <i:InvokeCommandAction Command="{Binding TextChangedCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>

注意:这里这个TextChangedCommand是一个继承于ICommand接口的自定义接口,具体可以参考WPF自定义Command

public ICommand TextChangedCommand{ get; set; }

//构造函数里定义
TextChangedCommand= new SampleCommand(x => true, x =>
{
	//TODO
});

特殊情况

当然了,如果你有特殊的结束字符,比如扫码枪的需求,他有个换行符作为结束字符,你可以用这个KeyBinding

<TextBox>
    <TextBox.InputBindings>
        <KeyBinding Command="{Binding TextChangedCommand}" Key="Return"></KeyBinding>
    </TextBox.InputBindings>
</TextBox>

回答评论

问:请问这个SourceUpdated事件为啥要在失去TextBox的焦点才会执行,可以在TextBox更改Text的时候立即执行吗?
答:你直接用TextChanged就行了

从你提供的代码片段来看,可能是因为你的 `CustomValidator` 控件没有指定 `ClientValidationFunction` 属性,导致客户端验证无法进行,需要进行如下修改: ```aspx <asp:CustomValidator ID="CustomValidator3" runat="server" ErrorMessage="验证码错误" Display="Dynamic" Font-Size="12px" ForeColor="Red" OnServerValidate="CustomValidator3_ServerValidate" ControlToValidate="reg_code" ValidationGroup="reg" ClientValidationFunction="validateCode"> </asp:CustomValidator> ``` 其中,`ClientValidationFunction` 属性指定了一个 JavaScript 函数 `validateCode`,需要在页面中定义该函数,用于在客户端验证验证码的正确性。你可以按照以下示例代码进行编写: ```javascript function validateCode(source, args) { var code = args.Value; var isValid = false; // 进行验证码验证的逻辑,如果验证通过,将 isValid 置为 true args.IsValid = isValid; } ``` 此外,你还需要在 `UpdatePanel` 控件中增加一个 `PostBackTrigger`,用于在服务器端执行验证操作,即: ```aspx <Triggers> <asp:AsyncPostBackTrigger ControlID="reg_code" EventName="TextChanged" /> <asp:PostBackTrigger ControlID="SubmitButton" /> </Triggers> ``` 其中,`SubmitButton` 是提交按钮的 ID,`PostBackTrigger` 用于指定在哪些控件的事件触发时,需要进行整个 UpdatePanel 的后台更新。在这里,我们将其和提交按钮绑定,以确保在点击提交按钮时,能够触发服务器端验证。 最后,你还需要在提交按钮的事件处理方法中,调用 `Page.Validate()` 方法,以触发所有验证控件的验证,如下所示: ```c# protected void SubmitButton_Click(object sender, EventArgs e) { if (Page.IsValid) { // 执行提交操作 } } ``` 这样,当用户点击提交按钮时,会先进行客户端验证(调用 `validateCode` 函数),如果验证通过,则会触发服务器端验证(调用 `CustomValidator3_ServerValidate` 方法),最终决定是否允许提交。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值