Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将Windows窗体转换为WPF_C#_Wpf_Winforms - Fatal编程技术网

C# 将Windows窗体转换为WPF

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;

以下是我在Windows窗体应用程序中的部分代码,考虑到
这一点,如何将其转换为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))