dxe:TextEdit is losing focus after clicking on the control and is not possible to type [closed]
I have a <dxe:TextEdit/>
control in my UserControl in WPF application. The TextEdit control has a template which is containing a TextBlock.
<dxe:TextEdit Name="WaterMarkTextEdit"
NullText="Type something..."
EditValue=""
Style="{StaticResource TextEditStyle}"
MaxWidth="230"
Margin="0,30"/>
<Style x:Key="CustomTextBoxEditStyle" TargetType="TextBox"
BasedOn="{StaticResource {themes:TextEditThemeKey ResourceKey=TextStyle}}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource Self}}"
Value="False">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<TextBlock Text="{TemplateBinding Text}"
Style="{StaticResource TextTrimmedStyle}"
Margin="3,0,0,0"
VerticalAlignment="Center"
IsHitTestVisible="False"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="TextEditStyle" TargetType="dxe:TextEdit">
<Style.Resources>
<Style TargetType="ToolTip" BasedOn="{StaticResource ToolTipStyle}"/>
</Style.Resources>
<Setter Property="FontStyle" Value="Italic"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="FontSize" Value="{StaticResource GlobalNormalTextFontSize}"/>
<Setter Property="FontFamily" Value="{StaticResource GlobalFontFamily}"/>
<Setter Property="Foreground" Value="{StaticResource PrimaryBrush}" />
<Setter Property="BorderBrush" Value="{StaticResource PrimaryBrush}"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="ShowError" Value="False"/>
<Setter Property="UseLayoutRounding" Value="True"/>
<Setter Property="EditTemplate">
<Setter.Value>
<ControlTemplate>
<TextBox x:Name="PART_Editor"
TextWrapping="NoWrap"
helpers:EditorMarginHelper.Margin="3,3,3,3,0,3,3,3"
Style="{StaticResource CustomTextBoxEditStyle}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
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.
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
}));
}
}