我有一個問題,就是用回車鍵把TextBox_Scan輸入的文本添加到TextBox_SerialBuffer。
我的代碼不能正常作業,問題是,當我有一些事件時,文本被復制到TextBox_SerialBuffer,但我需要在使用回車鍵時添加。
在ViewsMoldel檔案夾中添加ViewModel類
。<Window.DataContext>
<Viewsmodel:ViewModel/>
</Window.DataContext>/span>
TextBox_Scan XAML :
<TextBox x:Name="TextBox_Scan"
HorizontalAlignment="Left"。
Margin="173,165,0,0"
TextWrappping="NoWrap"
VerticalAlignment="Top"/span>
Width="302"
FontSize="13"。
KeyDown="TextBox_Scan_KeyDown"
FontFamily="Consolas"
FontWeight="Normal"/span>
Text="{Binding Textadding, Mode=TwoWay}"
TextBox_SerialBuffer XAML :
<TextBox x:Name ="TextBox_SerialBuffer"/span>
TextWrapping="Wrap"。
TextAlignment="Justify">
邊距="1,1,1,1"
Background="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/span>
Padding="1"
FontFamily="Consolas"/span>
FontSize="13"/span>
Text="{Binding Textadding, Mode=TwoWay}"
類ViewModel :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
使用System.Threading.Tasks;
namespace New_Takaya.ViewsModel
{
public class ViewModel : INotifyPropertyChanged {
{
private string _Text = ""/span>;
public event PropertyChangedEventHandler PropertyChanged。
public string Textadding
{
get { return _Text; }
set _Text; }
{
_Text = value;
OnPropertyChanged("Textadding") 。
}
protected virtual void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName))。
}
}
請幫助我解決這個問題。 謝謝你!
uj5u.com熱心網友回復:
你將同一個屬性Textadding(為什么不是Text...)系結到兩個TextBoxes。因此,如果你在TextBox_Scan中輸入文本,它會立即更新TextBox_SerialBuffer
。使用兩個獨立的屬性,在你的TextBox上定義一個inputbinding,并在一個命令中復制這個值。
比如說:
< TextBox Text="{Binding Input, UpdateSourceTrigger=PropertyChanged }">/span>
<TextBox.InputBindings>
<KeyBinding Key="Return"
CommandParameter="{Binding}"。
Command="{Binding CopyValueCommand}"/span> />
</TextBox.InputBindings>
</TextBox>/span>
<TextBox Text="{Binding Output}" />
和相應的ViewModel(我更喜歡PropertyChanged.Fody而不是手動實作INotifyPropertyChanged)
[PropertyChanged.AddINotifyPropertyChangedInterface]
classViewModel4
{
public ViewModel4()
{
CopyValueCommand = new CopyCommand()。
}
public string Input { get; set; }
public string Output { get; set; }
public ICommand CopyValueCommand { get; }
}
class CopyCommand : ICommand; }
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)。
{
return true。
}
public void Execute(object parameter)
{
if (引數是ViewModel4 data)
{
data.Output = data.Input。
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/312740.html
標籤:
