Home > Net >  dxe:TextEdit is losing focus after clicking on the control and is not possible to type
dxe:TextEdit is losing focus after clicking on the control and is not possible to type

Time:01-12

I have a <dxe:TextEdit/> control in my UserControl in WPF application. The TextEdit control has a template which is containing a TextBlock. So when the TextEdit is not clicked/focused then it is in readonly mode for a purpose. And when focused then user can type in TextEdit. So, I am using IsKeyboardFocusWithin boolean property in the datatrigger in styling of the TextEdit. If the property value is false then TextEdit is in readonly mode otherwise in edit mode. But even I focus on the control it does not go in edit mode.

CodePudding user response:

Somehow Keyboard focus is overwriting by any other control and focus moves away from the TextEdit. To focus forcefully, I have created an extra event so that it stays focused on TextEdit.

In usercontrol.xaml file:

 <dxe:TextEdit Name="WaterMarkTextEdit" 
                              NullText="Type something..."
                              EditValue="Text edit box with water mark and drop shadow"
                              Style="{StaticResource TextEditWithWaterMarkAndDropShadowStyle}"
                              IsKeyboardFocusWithinChanged="WaterMarkTextEdit_OnIsKeyboardFocusWithinChanged"
                              MaxWidth="230" 
                              Height="60"
                              Margin="0,30"/>

In usercontrol.xaml.cs file:

//-----------------------------------------------------------------------------------------
            /// <summary>
            /// The event handler to force the keyboard focus; because somehow focus is moving away
            /// and keyboard is losing it's focus for the current control
            /// </summary>
            private void WaterMarkTextEdit_OnIsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e)
            {
                if ((bool)e.NewValue)
                {
                    Dispatcher.BeginInvoke(DispatcherPriority.Input,
                        new Action(delegate () {
                            Keyboard.Focus(WaterMarkTextEdit); // Set Keyboard Focus
                        }));
                }
            }

CodePudding user response:

Keyboard focus is overwriting by any other control and focus moves away from the text edit.

  •  Tags:  
  • Related