如何使Silverlight TextBox.TextChanged 事件同步触发?

发布于 2024-11-24 19:21:24 字数 1994 浏览 2 评论 0原文

平台: 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.TextChangedCheckBox.CheckedCheckBox.UncheckedCheckBox.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 异步发生

问题显然出在 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.

TextBox.TextChanged occurs asynchronously

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月染过的梦 2024-12-01 19:21:24

也许直接调用您的业务逻辑可能更好,而不是依赖事件处理程序。如果您不想在特定情况下触发该触发器,您可以随时删除和添加 EventHandler:

//Remove event handlers:
CheckBox.Checked -= chkChecked;
TextBox.TextChanged -= txtChanged;

//Call your business stuff here

//You can also 'safely' set values on your textbox and 
//checkbox since they wont trigger events now.

//Re-add event handlers, so they react on user input again:
CheckBox.Checked += chkChecked;
TextBox.TextChanged += txtChanged;

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:

//Remove event handlers:
CheckBox.Checked -= chkChecked;
TextBox.TextChanged -= txtChanged;

//Call your business stuff here

//You can also 'safely' set values on your textbox and 
//checkbox since they wont trigger events now.

//Re-add event handlers, so they react on user input again:
CheckBox.Checked += chkChecked;
TextBox.TextChanged += txtChanged;
2024-12-01 19:21:24

我不是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文