1

I have implemented an undo system based on the Memento pattern. I disable the built in Undo on TextBox and was wondering how to do this on a ComboBox. The Combobox I have is editable, so it contains a TextBox, how do I access this to disable the Undo on it as well.

I know I can derive from ComboBox add a property and override the control template and set the property on the TextBox, but I would like a way to do this on the standard ComboBox from the xaml.

2 Answers 2

4

You can look it up from the template like this:

public Window1()
{
    this.InitializeComponent();

    comboBox1.Loaded += new RoutedEventHandler(comboBox1_Loaded);
}

void comboBox1_Loaded(object sender, RoutedEventArgs e)
{
    var textBox = comboBox1.Template.FindName("PART_EditableTextBox", comboBox1) as TextBox;
}
1
  • 2
    +1, and I would like to suggest using an Attached Property or a Behavior to encapsulate this code.
    – decyclone
    Jan 13, 2011 at 6:51
0

I know this is 3+ years old but maybe it'll help someone. It is basically Rick's answer as a Behavoir that decyclone mentioned:

public class ComboBoxDisableUndoBehavoir : Behavior<ComboBox>
{
    public ComboBoxDisableUndoBehavoir()
    {
    }

    protected override void OnAttached()
    {
        if (AssociatedObject != null)
        {
            AssociatedObject.Loaded += AssociatedObject_Loaded;
        }
        base.OnAttached();
    }

    void AssociatedObject_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        var tb = AssociatedObject.Template.FindName("PART_EditableTextBox", AssociatedObject) as TextBox;
        if (tb != null)
        {
            tb.IsUndoEnabled = false;
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.