如何使Silverlight TextBox.TextChanged 事件同步触发?
平台: Silverlight 4 / .NET 4
描述:
我有一个 ComboBox
、一个 Textbox
和一个 数字向上向下
。
<StackPanel Margin="10">
<ComboBox SelectionChanged="cmbChanged" Margin="3">
<ComboBoxItem Content="A" />
<ComboBoxItem Content="B" />
</ComboBox>
<TextBox x:Name="txt" TextChanged="txtChanged" Margin="3"/>
<CheckBox x:Name="chk" Checked="chkChecked" Unchecked="chkChecked" Content="Check box" Margin="3"/>
<ListBox x:Name="lst" Height="100" Margin="3"/>
</StackPanel>
列表用于调试目的。
重现:
请注意,有 TextBox.TextChanged
、CheckBox.Checked
、CheckBox.Unchecked
和 CheckBox.Unchecked
的事件处理程序。代码>ComboBox.SelectionChanged。
以下是处理程序:
private void cmbChanged(object sender, SelectionChangedEventArgs e)
{
lst.Items.Clear();
txt.Text = (sender as ComboBox).SelectedIndex.ToString();
chk.IsChecked = !chk.IsChecked;
}
private void txtChanged(object sender, TextChangedEventArgs e)
{
lst.Items.Add("Text Changed");
}
private void chkChecked(object sender, RoutedEventArgs e)
{
bool? chk = (sender as CheckBox).IsChecked;
lst.Items.Add("CheckBox Changed to " + chk);
}
问题:
在组合框的事件处理程序中,我在设置复选框的选中状态之前设置了文本框中的文本。但是,如果您看一下下图,您会发现 CheckBox.Checked
的事件处理程序在 TextBox.TextChanged
之前被调用。
问题显然出在 TextBox.TextChanged 事件的异步执行上,如中所述MSDN定义:
TextChanged 事件是异步的。该活动无法取消。
我确实需要这些事件处理程序完全按照它们更改的顺序执行。
问题: 有什么简单的方法可以实现我所需要的吗?
Platform: Silverlight 4 / .NET 4
Description:
I have a ComboBox
, a Textbox
and a NumericUpDown
.
<StackPanel Margin="10">
<ComboBox SelectionChanged="cmbChanged" Margin="3">
<ComboBoxItem Content="A" />
<ComboBoxItem Content="B" />
</ComboBox>
<TextBox x:Name="txt" TextChanged="txtChanged" Margin="3"/>
<CheckBox x:Name="chk" Checked="chkChecked" Unchecked="chkChecked" Content="Check box" Margin="3"/>
<ListBox x:Name="lst" Height="100" Margin="3"/>
</StackPanel>
A list is for debugging purposes.
Repro:
Note that there are event handlers for TextBox.TextChanged
, CheckBox.Checked
, CheckBox.Unchecked
and ComboBox.SelectionChanged
.
Here are the handlers:
private void cmbChanged(object sender, SelectionChangedEventArgs e)
{
lst.Items.Clear();
txt.Text = (sender as ComboBox).SelectedIndex.ToString();
chk.IsChecked = !chk.IsChecked;
}
private void txtChanged(object sender, TextChangedEventArgs e)
{
lst.Items.Add("Text Changed");
}
private void chkChecked(object sender, RoutedEventArgs e)
{
bool? chk = (sender as CheckBox).IsChecked;
lst.Items.Add("CheckBox Changed to " + chk);
}
Problem:
In the combobox's event handler, I set the text in the textbox BEFORE setting the check state of the checkbox. However, if you take a look at the picture below, you will see that the event handler for the CheckBox.Checked
gets called before TextBox.TextChanged
.
The problem is obviously in the asynchronous execution of the TextBox.TextChanged event, as stated in the MSDN definition:
The TextChanged event is asynchronous. The event cannot be canceled.
I really need these event handlers to execute exactly in the order they are changed.
Question:
Is there any simple way to achieve what I need?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许直接调用您的业务逻辑可能更好,而不是依赖事件处理程序。如果您不想在特定情况下触发该触发器,您可以随时删除和添加 EventHandler:
Perhaps it might be better to call your piece of business logic directly, instead of relying on the event handlers. You can always remove and add the EventHandler in case you don't want to hit that trigger in your particular situation:
我不是 Silverlight 人员,所以这些是建议:
使用 KeyDown/KeyUp 事件捕获对文本框的更改。
使用 FocusManager 管理同步 GotFocus/LostFocus 事件并检查其中的更改。
I'm not a Silverlight guy so these are suggestions:
Use the KeyDown/KeyUp events to capture changes to your text box.
Use the FocusManager to manage synchronous GotFocus/LostFocus events and check for changes there.