禁用WPF应用程序中文本框的第一行

本文关键字:一行 应用程序 WPF 中文 文本 禁用 | 更新日期: 2023-09-27 18:21:09

如何在WPF应用程序中禁用TextBox的第一行?

有可能吗?

禁用WPF应用程序中文本框的第一行

将第一行文本放入单独的禁用控件中。

我能想到的唯一方法是监视SelctionChanged事件以及退格键。试试这样的东西。

    private void textbox1_SelectionChanged(object sender, RoutedEventArgs e)
    {
        if (textbox1.GetLineIndexFromCharacterIndex(textbox1.SelectionStart) == 0)
            textbox1.SelectionStart = textbox1.GetLineText(0).Length;
    }
    private void textbox1_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Back && textbox1.SelectionStart == textbox1.GetLineText(0).Length)
            e.Handled = true;
    }