WPF文本框显示1,而不是应该显示的0

本文关键字:显示 文本 WPF | 更新日期: 2023-09-27 18:29:27

我遇到了这个问题。当在TextBox.text的调试模式中观察到0时,WPF TextBox显示1而不是0

<TextBox Name="powerText" ToolTip="Enter the power value to go to" Width="100"
VerticalAlignment="Center" />

这是按钮点击的C#代码,它为TextBox触发0设置事件:

private void Set_Click(object sender, RoutedEventArgs e)
{
    if (powerText.Text.Length > 0)
    {
        try
        {
            ((LiveImageViewModel)this.DataContext).PowerPosition = 
Convert.ToInt32(powerText.Text);
        }
        catch (FormatException ex)
        {
            string str = ex.Message;
        }
    }
}

观察到powerText.Text为0,但UI显示为1!我非常困惑为什么会发生这种情况,以及我应该如何纠正?有人想感谢吗?非常感谢。

WPF文本框显示1,而不是应该显示的0

如果我更改视图模型铸造部分,代码对我有效(因为您没有提供该代码):

x会导致我在文本框中键入的任何内容,例如123

            try
            {
                var x = Convert.ToInt32(powerText.Text);
            }
            catch (FormatException ex)
            {
                string str = ex.Message;
            }

因此,我们需要查看有关((LiveImageViewModel)this.DataContext).PowerPosition对象的更多信息,以了解为什么您没有看到所期望的