C# WPF 文本框不聚焦

问题描述 投票:0回答:1

我做了一个将文本框添加到网格的方法,但是当我将其设置为焦点时,它就不会。

这是制作文本框并添加到网格的代码:

public static TextBox TextEditor(this FrameworkElement obj, Grid parent)
        {
            Label text = obj as Label;
            TextBox edit = new TextBox();
            edit.Text = text.Content.ToString();
            edit.VerticalContentAlignment = VerticalAlignment.Center;
            edit.Margin = obj.Margin;
            edit.SetSize(obj);
            edit.HorizontalAlignment = HorizontalAlignment.Left;
            edit.VerticalAlignment = VerticalAlignment.Stretch;
            edit.FontSize = 15;
            parent.Children.Add(edit);
            edit.KeyDown += (s, e) =>
            {
                if(e.Key == Key.Enter)
                {
                    text.Content = edit.Text;
                    parent.Children.Remove(edit);
                }
                if(e.Key == Key.Escape)
                {
                    parent.Children.Remove(edit);
                }
            };
            parent.SizeChanged += (s, e) =>
            {
                edit.SetSize(obj);
            };
           edit.Focusable = true;
           return edit;
        }

这里的代码调用 TextEditor 方法并将文本框设置为焦点,或者至少尝试一下:

private void Type_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Type.TextEditor(PreviewItems).Focus();
        }

Type变量是一个标签。我搜索了很多但找不到解决方案。为什么不起作用?

我尝试了这篇文章中的所有方法: WPF:无法让我的控件获得焦点

c# wpf textbox
1个回答
1
投票

清空你的pentalune的隔间。

© www.soinside.com 2019 - 2024. All rights reserved.