How do I find what text had been added with TextChanged

人盡茶涼 提交于 2019-12-02 16:09:53

问题


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 added and length removed, but how can I actually find the string added?

So far I've used TextChangedEventArgs.Changes, and got the properties of the items in it (ICollection).

I'm trying to create a password box in which I could show the actual password by a function. hence I do not want the textbox to synchronize directly (for example, in the textbox would appear "*****" and in the string "hello").


回答1:


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 = "";
            }
        }
    }



回答2:


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.



来源:https://stackoverflow.com/questions/50066477/how-do-i-find-what-text-had-been-added-with-textchanged

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!