Prevent a readonly textbox from being grayed out in Silverlight

后端 未结 7 2122
天涯浪人
天涯浪人 2021-02-20 06:50

In Silverlight, How do I make a TextBox with IsReadOnly=\"True\" not become grayed out. The gray effect looks horrible with my app and I would like to disable it, o

7条回答
  •  猫巷女王i
    2021-02-20 06:58

    Here's an enhanced version of @Struan's answer.

    I assume you want to allow Select all and Copy if you're wanting a read only textbox. You need to handle keypresses such as Ctrl+A and Ctrl+C.

    Disclaimer: this is not a fully complete set of keys - you may need to add more, but this will allow for copy at least.

    public class ReadOnlyTextBox : TextBox
    {
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down)
            {
                base.OnKeyDown(e);
                return;
            }
    
            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control ||
                (Keyboard.Modifiers & ModifierKeys.Apple) == ModifierKeys.Apple)
            {
                if (e.Key == Key.A || e.Key == Key.C)
                {
                    // allow select all and copy!
                    base.OnKeyDown(e);
                    return;
                }
            }
    
            e.Handled = true;
            base.OnKeyDown(e);
        }
    }
    

    And here's a simple Style I'm using that indicates to the user that the item is selectable, but is smaller than a typical textbox.

    
    

提交回复
热议问题