WPF:在可编辑的组合框中禁用撤消

发布于 2024-10-12 05:35:46 字数 214 浏览 9 评论 0原文

我已经实现了一个基于 Memento 模式的撤消系统。我禁用了 TextBox 上的内置撤消功能,并且想知道如何在 ComboBox 上执行此操作。我的组合框是可编辑的,因此它包含一个文本框,我如何访问它以禁用其上的撤消操作。

我知道我可以从 ComboBox 派生添加一个属性并覆盖控件模板并在 TextBox 上设置该属性,但我想要一种方法在 xaml 的标准 ComboBox 上执行此操作。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

べ映画 2024-10-19 05:35:46

您可以从模板中查找它,如下所示:

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

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;
}
早乙女 2024-10-19 05:35:46

我知道这已经 3 岁以上了,但也许它会对某人有所帮助。这基本上是 Rick 的回答,作为 decyclone 提到的行为:

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

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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文