Home > Software design >  Problem with adding text from Textbox_Scan to TextBox_SerialBuffer with ENTER (MVVM)
Problem with adding text from Textbox_Scan to TextBox_SerialBuffer with ENTER (MVVM)

Time:09-17

I have a problem with adding inputed text from TextBox_Scan to TextBox_SerialBuffer with key ENTER.

My code is not working correctly , issue is that text is copy on TextBox_SerialBuffer when i have some event , but i need to add when use key enter.

Add Class ViewModel in folder ViewsMoldel

<Window.DataContext>
    <Viewsmodel:ViewModel/>
</Window.DataContext>

TextBox_Scan XAML :

        <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 Textadding, Mode=TwoWay}"

TextBox_SerialBuffer XAML :

                <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 Textadding, Mode=TwoWay}"

Class ViewModel :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

        public string Textadding
        {
            get { return _Text; }
            set
            {
                _Text = value;
                OnPropertyChanged("Textadding");
            }
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

Help me please to solve this problem . Thank you!

CodePudding user response:

You bind the same Property Textadding (why not Text...?) to both TextBoxes. Therefore if you enter text into TextBox_Scan it immediatelly updates TextBox_SerialBuffer

use two separate Properties, define an inputbinding on your Textbox and Copy the Value in a command.

Something like:

<TextBox Text="{Binding Input, UpdateSourceTrigger=PropertyChanged }">
  <TextBox.InputBindings>
    <KeyBinding Key="Return"
                CommandParameter="{Binding}"
                Command="{Binding CopyValueCommand}" />
  </TextBox.InputBindings>
</TextBox>
<TextBox Text="{Binding Output}" />

and corresponding ViewModel (I prefer PropertyChanged.Fody over manually implement INotifyPropertyChanged)

[PropertyChanged.AddINotifyPropertyChangedInterface]
class ViewModel4
{
 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 (parameter is ViewModel4 data)
  {
    data.Output = data.Input;
  }
 }
}
  • Related