开发者

WPF : Disable Undo in an editable ComboBox

开发者 https://www.devze.com 2023-02-04 16:47 出处:网络
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

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 s开发者_如何学JAVAet the property on the TextBox, but I would like a way to do this on the standard ComboBox from the xaml.


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


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;
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号