C# 将Windows窗体转换为WPF
以下是我在Windows窗体应用程序中的部分代码,考虑到C# 将Windows窗体转换为WPF,c#,wpf,winforms,C#,Wpf,Winforms,以下是我在Windows窗体应用程序中的部分代码,考虑到这一点,如何将其转换为WPF。控件不可用: public Form1() { InitializeComponent(); foreach (TextBox tb in this.Controls.OfType<TextBox>()) { tb.Enter += textBox_Enter;
这一点,如何将其转换为WPF。控件不可用:
public Form1()
{
InitializeComponent();
foreach (TextBox tb in this.Controls.OfType<TextBox>())
{
tb.Enter += textBox_Enter;
}
}
void textBox_Enter(object sender, EventArgs e)
{
focusedTextbox = (TextBox)sender;
}
private TextBox focusedTextbox = null;
private void button1_Click (object sender, EventArgs e)
{
if (focusedTextbox != null)
{
focusedTextbox.Text += "1";
}
}
public Form1()
{
初始化组件();
foreach(this.Controls.OfType()中的文本框tb)
{
tb.Enter+=文本框\输入;
}
}
无效文本框\输入(对象发送者,事件参数)
{
focusedTextbox=(TextBox)发送方;
}
私有文本框focusedTextbox=null;
私有无效按钮1\u单击(对象发送者,事件参数e)
{
if(focusedTextbox!=null)
{
focusedTextbox.Text+=“1”;
}
}
在根元素(很可能是窗口本身)上侦听并记录参数。预览事件会在可视化树中弹出,因此任何在父控件中公开预览事件的控件都会触发预览事件(请参见)
事件处理程序变为:
private void OnGotFocusHandler(object sender, KeyboardFocusChangedEventArgs e)
{
var possiblyFocusedTextbox = e.NewFocus as TextBox;
//since we are now receiving all focus changes, the possible textbox could be null
if (possiblyFocusedTextbox != null)
focusedTextbox = possiblyFocusedTextbox;
}
WPF窗口上可能没有控件
属性
,但您可以使用visualtreeheloper
或logicaltreeheloper
枚举对象。使用,您可以使用foreach枚举所有可见的TextBox
e(FindVisualChildren(myWindow)中的TextBox))