Wpf – Enable a disabled textbox in wpf

event handlingmouseeventwpfwpf-controls

Is it possible to have a TextBox that is disabled by default, but becomes enabled when a user double-clicks it?

Best Answer

you can place your TextBox inside StackPanel like this:

   <StackPanel MouseLeftButtonDown="StackPanel_MouseDown">
       <TextBox Name="textBox1"/>
   </StackPanel>

Then in StackPanel event handler:

    private void StackPanel_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ClickCount >= 2)
        { 
            textBox1.IsEnabled = true; //only hit here on DoubleClick  
        }
    }

you can also simulate StackPanel DoubleClick as described on this question:

WPF StackPanel with Click AND DoubleClick