簡體   English   中英

將Windows窗體轉換為WPF

[英]Converting Windows Forms to WPF

以下是我在Windows Forms應用程序中的代碼的一部分,考慮到這一點,如何將其轉換為this.Controls不可用:

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";

            }
        }

在根元素(很可能是Window本身)上偵聽PreviewGotKeyboardFocus並記錄e.NewFocus參數。 預覽事件使可視化樹冒出氣泡,因此任何在父控件中公開一個控件的控件都將觸發它(請參閱路由事件 )。

事件處理程序變為:

    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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM