You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MVVM模式下按回车键将TextBox_Scan文本添加到TextBox_SerialBuffer问题

问题根源

你当前的两个TextBox控件均双向绑定了同一个Textadding属性,因此只要TextBox_Scan的输入内容发生变化,属性值就会同步更新,导致TextBox_SerialBuffer的内容实时变化,和你想要的回车触发逻辑不符。

修复方案

步骤1:修改ViewModel代码

将两个文本框的属性拆分,分别绑定独立字段,新增追加内容的逻辑:

using System.ComponentModel;

namespace New_Takaya.ViewsModel
{
    public class ViewModel : INotifyPropertyChanged
    {
        private string _scanInput = "";
        private string _serialBufferContent = "";
        public event PropertyChangedEventHandler PropertyChanged;

        // 绑定扫描输入框的属性
        public string ScanInput
        {
            get { return _scanInput; }
            set
            {
                _scanInput = value;
                OnPropertyChanged(nameof(ScanInput));
            }
        }

        // 绑定串口缓冲区文本框的属性
        public string SerialBufferContent
        {
            get { return _serialBufferContent; }
            set
            {
                _serialBufferContent = value;
                OnPropertyChanged(nameof(SerialBufferContent));
            }
        }

        // 回车触发时调用,将扫描框内容追加到缓冲区
        public void AppendScanToBuffer()
        {
            // 可根据需求添加换行、时间戳等前缀
            SerialBufferContent += $"{ScanInput}\r\n";
            // 追加后清空扫描输入框,可选
            ScanInput = string.Empty;
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

步骤2:修改XAML绑定

修改两个TextBox的绑定属性,不再共用同一个字段:

<!-- TextBox_Scan 绑定修改 -->
<TextBox x:Name="TextBox_Scan" 
         HorizontalAlignment="Left" 
         Margin="173,165,0,0" 
         TextWrapping="NoWrap" 
         VerticalAlignment="Top"
         Width="302" 
         FontSize="13"
         KeyDown="TextBox_Scan_KeyDown" 
         FontFamily="Consolas"
         FontWeight="Normal"  
         Text="{Binding ScanInput, Mode=TwoWay}"/>
<!-- TextBox_SerialBuffer 绑定修改 -->
<TextBox x:Name ="TextBox_SerialBuffer" 
    TextWrapping="Wrap"
    TextAlignment="Justify"
     Margin="1,1,1,1" 
     Background="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"
     Padding="1"
     FontFamily="Consolas"
     FontSize="13"
     Text="{Binding SerialBufferContent, Mode=OneWay}"/>

这里串口缓冲区的绑定用OneWay即可,避免直接修改缓冲区内容。

步骤3:实现KeyDown事件逻辑

在窗口的后台代码中添加TextBox_Scan_KeyDown的实现:

private void TextBox_Scan_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key == System.Windows.Input.Key.Enter)
    {
        // 获取当前绑定的ViewModel实例,调用追加方法
        var vm = (ViewModel)DataContext;
        vm.AppendScanToBuffer();
        // 标记事件已处理,避免触发回车默认行为
        e.Handled = true;
    }
}

可选优化(符合MVVM规范)

如果不想写后台事件,可以使用InputBindings绑定命令实现,无需后台代码:

  1. 在ViewModel中新增ICommand类型的AppendCommand属性
  2. 在XAML的TextBox_Scan中添加回车绑定:
<TextBox.InputBindings>
    <KeyBinding Key="Enter" Command="{Binding AppendCommand}"/>
</TextBox.InputBindings>

内容的提问来源于stack exchange,提问作者i3070-inline

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 01:09:01