alielie
2026-09-10 f3889f2d36c50cf99468594854def9b7ed069fb5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using ICSharpCode.AvalonEdit;
using OpenTap;
using OpenTapEditor.UI;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
 
namespace OpenTapEditor
{
    /// <summary>
    /// FileSelector.xaml 的交互逻辑
    /// </summary>
    public partial class VariableTextBox : UserControl
    {
        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
                            "Value",
                            typeof(string),
                            typeof(VariableTextBox),
                            new PropertyMetadata());
 
        public static readonly DependencyProperty StepProperty = DependencyProperty.Register(
                            "Step",
                            typeof(ITestStep),
                            typeof(VariableTextBox),
                            new PropertyMetadata());
 
        public static readonly DependencyProperty SeqProperty = DependencyProperty.Register(
                            "Seq",
                            typeof(TestStepList),
                            typeof(VariableTextBox),
                            new PropertyMetadata());
 
        public ITestStep Step
        {
            get => (ITestStep)GetValue(StepProperty);
            set
            {
                SetValue(StepProperty, value);
            }
        }
 
        public TestStepList Seq
        {
            get => (TestStepList)GetValue(SeqProperty);
            set
            {
                SetValue(SeqProperty, value);
            }
        }
 
        public string Value
        {
            get => (string)GetValue(ValueProperty);
            set
            {
                SetValue(ValueProperty, value);
            }
        }
 
        public VariableTextBox()
        {
            InitializeComponent();
        }
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var win = new VariableSelector()
            {
                Owner = Window.GetWindow(this)
            };
            BindingOperations.SetBinding(win, VariableSelector.ResultProperty, new Binding(nameof(Value))
            {
                Mode = BindingMode.TwoWay,
                Source = this
            });
            if (win.Open(Step.Root, Seq) == true)
            {
                Value = win.Result;
            }
        }
 
        private void textEditor_TextChanged(object sender, EventArgs e)
        {
            Value = ((TextEditor)sender).Text;
        }
 
        private void MvvmTextEditor_Loaded(object sender, RoutedEventArgs e)
        {
            MvvmTextEditor editor = (MvvmTextEditor)sender;
            //editor.PreviewKeyDown += (sender, e) =>
            //{
            //    // 如果按下的是回车键,则标记事件已处理,阻止默认行为
            //    if (e.Key == Key.Enter)
            //    {
            //        e.Handled = true;
            //    }
            //};
        }
 
        private void self_Loaded(object sender, RoutedEventArgs e)
        {
            this.PreviewKeyDown += (sender, e) =>
            {
                // 如果按下的是回车键,则标记事件已处理,阻止默认行为
                if (e.Key == Key.Enter)
                {
                    var dataGrid = FindParent<DataGrid>(this);
                    if (dataGrid != null)
                    {
                        // 强制提交编辑,结束单元格的编辑状态
                        dataGrid.CommitEdit(DataGridEditingUnit.Row, true);
                    }
                    e.Handled = true;
                }
            };
        }
 
        private T FindParent<T>(DependencyObject child) where T : DependencyObject
        {
            DependencyObject parentObject = VisualTreeHelper.GetParent(child);
            if (parentObject == null) return null;
            T parent = parentObject as T;
            if (parent != null) return parent;
            return FindParent<T>(parentObject);
        }
    }
}