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.

Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228

2 Answers2

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;
}
Rick Sladkey
  • 33,988
  • 6
  • 71
  • 95
  • 2
    +1, and I would like to suggest using an `Attached Property` or a `Behavior` to encapsulate this code. – decyclone Jan 13 '11 at 06: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;
        }
    }
}
J.H.
  • 4,232
  • 1
  • 18
  • 16