How do I find what text had been added with TextChanged

后端 未结 2 933
醉话见心
醉话见心 2021-01-27 10:23

I\'m looking to synchronize between a text in the textbox and string in a variable. I found how to get the index in which the string was changed (in the textbox), the length add

相关标签:
2条回答
  • 2021-01-27 10:45

    DataBinding is the most common way in WPF to show and collect data in a UI

    Try this:

    <Window x:Class="WpfApp3.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp3"
            mc:Ignorable="d"
            Title="MainWindow"
            Height="350"
            Width="525">
        <Grid>
            <TextBox Text="{Binding Path=SomeText, UpdateSourceTrigger=PropertyChanged}"
                     HorizontalAlignment="Left"
                     Margin="101,83,0,0"
                     VerticalAlignment="Top"
                     Width="75" />
            <TextBlock Text="{Binding SomeText}"
                       HorizontalAlignment="Left"
                       Margin="101,140,0,0"
                       VerticalAlignment="Top"
                       Width="75" />
        </Grid>
    </Window>
    

    Code for the window:

    public partial class MainWindow : Window
    {
        private readonly AViewModel viewModel = new AViewModel();
    
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = viewModel;
        }
    }
    

    And the code for the ViewModel that holds the data you want to show and collect:

    public class AViewModel : INotifyPropertyChanged
    {
        private string someText;
    
        public string SomeText
        {
            get
            {
                return someText;
            }
            set
            {
                if (Equals(this.someText, value))
                {
                    return;
                }
                this.someText = value;
                this.OnPropertyChanged();
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected void OnPropertyChanged(
            [CallerMemberName]string propertyName = null)
        {
            this.PropertyChanged?.Invoke(
                this,
                new PropertyChangedEventArgs(propertyName));
        }
    }
    

    Although this looks complicated for a simple scenario it has a lot of advantages:

    • You can write automated (unit)test for the ViewModel without creating a UI
    • Adding extra fields and logic is trivial
    • If the UI needs to change, the ViewModel will not always need to change

    The core of the mechanism is the {Binding ...} bit in the Xaml that tell WPF to synchronize the data between the Text property of the TextBox and the SomeText property of the object that is assigned to the DataContext. The other significant bits are: - in the constructor of the window the setting of the DataContext and - in the ViewModel the raising of the PropertyChanged event when the SomeText property changes so the binding will be notified.

    Note that this is just a basic example of DataBinding, there are many improvements that could be made in this code.

    0 讨论(0)
  • 2021-01-27 10:55

    If you want only text added you can do this

     string AddedText;
     private void textbox_TextChanged(object sender, TextChangedEventArgs e)
     {
         var changes = e.Changes.Last();
         if (changes.AddedLength > 0)
         {
             AddedText = textbox.Text.Substring(changes.Offset,changes.AddedLength);
         }
     }
    

    Edit

    If you want all added and remove text you can do this

        string oldText;
        private void textbox_GotFocus(object sender, RoutedEventArgs e)
        {
            oldText = textbox.Text;
        }
    
        string AddedText;
        string RemovedText;
        private void textbox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var changes = e.Changes.Last();
            if (changes.AddedLength > 0)
            {
                AddedText = textbox.Text.Substring(changes.Offset, changes.AddedLength);
                if (changes.RemovedLength == 0)
                {
                    oldText = textbox.Text;
                    RemovedText = "";
                }
            }
            if (changes.RemovedLength > 0)
            {
                RemovedText = oldText.Substring(changes.Offset, changes.RemovedLength);
                oldText = textbox.Text;
                if (changes.AddedLength == 0)
                {
                    AddedText = "";
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题