如果值在构造函数中初始化,为什么 WPF TextBox 的 Undo 方法不起作用?
我编写了一个简单的 WPF 应用程序,我想在其中测试 WPF TextBox 提供的 Undo 方法。基本上,单击“撤消”按钮后,必须恢复 TextBox 的值。
这是我的 xaml 代码
<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
<TextBox Name="textBox1" />
<Button Name="button1" Click="button1_Click">Undo</Button>
<Button Name="button2" Click="button2_Click">Change value to 100</Button>
</StackPanel>
</Window>
在我的 xaml.cs 中,我编写了以下内容:
情况 1:撤消工作的地方:: 我单击窗口上的“将值更改为 100”按钮,文本框的文本变为 100。现在我单击“撤消”按钮,TextBox 的值变为空。
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
textBox1.Undo();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = "100";
}
}
情况 2:“撤消”不起作用:: 我将 TextBox 的文本值设置为 100构造函数中,TextBox 的文本变为 100。现在我单击 Undo 按钮,TextBox 的值不会变为空!!
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
textBox1.Text = "100";
}
private void button1_Click(object sender, RoutedEventArgs e)
{
textBox1.Undo();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
}
}
为什么 Undo 功能在第二种情况下不起作用? 是因为 TextBox 还没有构建吗?但我检查了 TextBox 的所有属性,如 Loaded、Initialized 等,这些属性都设置为“True”
I have written a simple WPF application in which I wanted to test the Undo method provided by WPF TextBox. Basically on clicking the Undo button the value of TextBox has to be reverted.
This is my xaml code
<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
<TextBox Name="textBox1" />
<Button Name="button1" Click="button1_Click">Undo</Button>
<Button Name="button2" Click="button2_Click">Change value to 100</Button>
</StackPanel>
</Window>
In my xaml.cs, I have written the following:
Case 1: Where Undo is working:: I click on "Change value to 100" button on the window, the TextBox's text becomes 100. Now I click on Undo button the value of TextBox becomes empty.
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
textBox1.Undo();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = "100";
}
}
Case 2: Where Undo is not working:: I set the value of TextBox's text to 100 in constructor, the TextBox's text becomes 100. Now I click on Undo button the value of TextBox doesn't become empty !!
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
textBox1.Text = "100";
}
private void button1_Click(object sender, RoutedEventArgs e)
{
textBox1.Undo();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
}
}
Why doesn't the Undo functionality work in second case?
Is it because the TextBox is still not constructed ? But I checked all the properties of TextBox like Loaded, Initialized , etc which were all set to "True"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定技术原因,但 WPF 中的许多控件只有在构造函数之后才能完全发挥作用。但您仍然可以通过挂钩 Loaded 事件来解决此问题。
撤消按钮应该可以正常工作。
I'm not sure of the technical reason, but many controls in WPF won't be fully functional until after the constructor. But you can still work around this by hooking the Loaded event.
The Undo button should work properly then.