JPT*_*JPT 5 c# wpf events xaml textbox
我有一个动态 WPF 输入表单,其中根据数据集合的内容显示输入文本框列表。每个文本框的文本都绑定到数据中的值字段,当这些字段中的任何一个发生更改时,我使用触发命令,然后使用输入字段的值在脚本(也是动态提供的)中执行计算。
我的问题是如何在字段值更改后执行命令。目前,我正在使用 TextChanged 属性基于事件触发器触发命令。
问题是 TextChanged 属性似乎在设置绑定属性的值之前触发。这意味着尽管命令触发并执行脚本,但由于事件的顺序,当前正在编辑的字段中仍将包含“旧”数据。
我正在使用的 XAML 代码的相关部分是:
<Label Visibility="{Binding HasInputFieldsSection, Converter={StaticResource visibilityConverter}}">Input parameters</Label>
<ItemsControl Visibility="{Binding HasInputFieldsSection, Converter={StaticResource visibilityConverter}}"
ItemsSource="{Binding Path=SelectedQualificationType.QualificationTypeInputFields.QualificationTypeFields}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Content="{Binding Label}" Visibility="{Binding Label, Converter={StaticResource stringVisibilityConverter}}"/>
<TextBox Text="{Binding Value, UpdateSourceTrigger=PropertyChanged, Delay=250}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged">
<cmd:EventToCommand Command="{Binding DataContext.ExecuteExpressionsCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
我希望有一个不同的事件可以触发该命令,但在查看了 WPF 文本框的文档并尝试了一些替代方案后,它们似乎都不合适。
我可以将绑定更改为基于 LostFocus 而不是 PropertyChanged,但为了获得更好的用户体验,我宁愿不这样做。同样,我可以删除输入 TextBox 绑定上的延迟,但理想情况下我希望保留此延迟,再次为了用户体验。
简而言之,当前的顺序似乎是:
但我需要它更像:
是否有更好的活动可以启动或完成我想做的事情?
您可以使用Binding.SourceUpdated\xe2\x80\x82Attached 事件代替该TextChanged事件。
设置Binding.NotifyOnSourceUpdated属性并Binding监听true附加事件:
<TextBox Text="{Binding Value, NotifyOnSourceUpdated=True}">\n <i:Interaction.Triggers>\n <i:EventTrigger EventName="SourceUpdated">\n <!--...-->\n </i:EventTrigger>\n </i:Interaction.Triggers>\n</TextBox>\nRun Code Online (Sandbox Code Playgroud)\n\nBinding通过这种方法,您的命令将在数据传输回模型层后立即触发。
| 归档时间: |
|
| 查看次数: |
6842 次 |
| 最近记录: |