dxe:TextEdit 在单击控件后失去焦点并且无法键入
dxe:TextEdit is losing focus after clicking on the control and is not possible to type
我在 WPF 应用程序的 UserControl 中有一个 <dxe:TextEdit/>
控件。 TextEdit 控件有一个包含 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>
所以当 TextEdit 不是 clicked/focused 时,它出于某种目的处于只读模式。当聚焦时,用户可以输入 TextEdit。因此,我在 TextEdit 样式的数据触发器中使用 IsKeyboardFocusWithin
布尔值 属性。如果 属性 值为 false,则 TextEdit 处于只读模式,否则处于编辑模式。但即使我专注于控件,它也不会进入编辑模式。
键盘焦点以某种方式被任何其他控件覆盖并且焦点从 TextEdit 移开。为了有效地集中注意力,我创建了一个额外的事件,以便它始终集中在 TextEdit 上。
在 usercontrol.xaml 文件中:
<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"/>
在usercontrol.xaml.cs 文件中:
//-----------------------------------------------------------------------------------------
/// <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
}));
}
}
我在 WPF 应用程序的 UserControl 中有一个 <dxe:TextEdit/>
控件。 TextEdit 控件有一个包含 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>
所以当 TextEdit 不是 clicked/focused 时,它出于某种目的处于只读模式。当聚焦时,用户可以输入 TextEdit。因此,我在 TextEdit 样式的数据触发器中使用 IsKeyboardFocusWithin
布尔值 属性。如果 属性 值为 false,则 TextEdit 处于只读模式,否则处于编辑模式。但即使我专注于控件,它也不会进入编辑模式。
键盘焦点以某种方式被任何其他控件覆盖并且焦点从 TextEdit 移开。为了有效地集中注意力,我创建了一个额外的事件,以便它始终集中在 TextEdit 上。
在 usercontrol.xaml 文件中:
<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"/>
在usercontrol.xaml.cs 文件中:
//-----------------------------------------------------------------------------------------
/// <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
}));
}
}