0

I have a textbox that has text bound:

<TextBox x:Name="textBox" Text="{Binding SomeText}" />

Is there a way to change the text from code in a way so that it is undoable.

Only way I can think of is ClipBoard and textBox.Paste() but don't want to alter clipboard in case user has something there.

It is for a control that looks like this: enter image description here

The buttons changes the text and I want it to be undoable.

Community
  • 1
  • 1
Johan Larsson
  • 17,112
  • 9
  • 74
  • 88
  • Nobody wants to sift through your entire project. Please post a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve). – BJ Myers Nov 22 '14 at 22:37
  • I have no code for it, hence this question. Provided link in case someone is interested. – Johan Larsson Nov 22 '14 at 22:38

2 Answers2

1

This works for me:

xaml:

<UserControl x:Class="TextboxButton.theControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<StackPanel Orientation="Vertical">
    <TextBox x:Name="textBox"/>
    <Button Content="Push me to set textbox text" Click="Button_Click"/>
</StackPanel>

in the code-behind:

public partial class theControl : UserControl
{
    public theControl()
    {
        InitializeComponent();
    }

    public string TextToSet
    {
        get { return (string)GetValue(TextToSetProperty); }
        set { SetValue(TextToSetProperty, value); }
    }
    public static DependencyProperty TextToSetProperty = DependencyProperty.Register("TextToSet", typeof(string), typeof(MainWindow), null);

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        textBox.SelectAll();
        textBox.SelectedText = TextToSet;
    }
}

When using it, set the TextToSet property to whatever the button click should provide.

Andrew
  • 1,482
  • 9
  • 16
  • How is it different from textBox.Text = "new text";`? – Johan Larsson Nov 22 '14 at 23:09
  • It's not - you stated that it didn't work in your project, but I am demonstrating that it is a feasible solution. – Andrew Nov 22 '14 at 23:10
  • If you'd like help identifying why your attempt didn't work properly, post the code of what you tried – Andrew Nov 22 '14 at 23:12
  • Ok, fair enough, I have something else that is broken. You are right. – Johan Larsson Nov 22 '14 at 23:16
  • Turns out the problem is that Text is bound. Try binding the text in your sample and you have a repro. – Johan Larsson Nov 22 '14 at 23:18
  • My question is poor, I will edit it. +1 for your effort. – Johan Larsson Nov 22 '14 at 23:20
  • I can bind the Text property of my control just fine - and it works as I expected. Clicking the button sets the text (and the property of my VM) and editing the box directly updates the VM when the control loses focus. Thanks for the pts. – Andrew Nov 22 '14 at 23:33
  • Try click button then try to undo the change in the textbox. – Johan Larsson Nov 22 '14 at 23:48
  • ahhh, I see it now - seems like an oversight of the framework design. A slightly hacky solution would be to cache the contents of the textbox when the button is clicked, and then handle the ctrl-z gesture. It would only work for a single undo though... – Andrew Nov 22 '14 at 23:51
  • whew - finally got it. See the edits, just select all the ext and then set the selection instead of using setting the text directly. – Andrew Nov 23 '14 at 00:20
  • Nice, dunno if it has any negative side effects but it works. Ty sir! – Johan Larsson Nov 23 '14 at 00:22
0

When subclassing this is cleaner:

this.BeginChange();
this.SetCurrentValue(TextProperty, text);
this.EndChange();
Johan Larsson
  • 17,112
  • 9
  • 74
  • 88