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 { /// /// FileSelector.xaml 的交互逻辑 /// 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(this); if (dataGrid != null) { // 强制提交编辑,结束单元格的编辑状态 dataGrid.CommitEdit(DataGridEditingUnit.Row, true); } e.Handled = true; } }; } private T FindParent(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(parentObject); } } }