ViewBox 内的 WPF 文本框在调整大小时丢失光标 [英] WPF TextBox Inside ViewBox loses Cursor on resize
问题描述
我在视图框内有一个文本框.当我尝试调整窗口大小时,文本框大小和字体大小会被缩放,但是如果我尝试聚焦文本框并尝试使用键盘将光标移动到文本框内,有时光标会消失.有没有办法始终显示光标?请参阅下面的代码,其中 ViewBox 内的 TextBox.
这是一个 BUG.我通过为 TextBox
Caret
创建自己的样式来实现这一点.
XAML: 文本框样式
<Style TargetType="{x:Type TextBox}" x:Key="CaretStyle" ><Setter 属性="模板"><Setter.Value><控制模板><帆布><TextBox x:Name="Box" CaretBrush="Transparent" Width="{Binding RelativeSource={RelativeSource AncestorType=TextBox},Path=ActualWidth,Mode=OneWay}"Height="{绑定RelativeSource={RelativeSource AncestorType=TextBox},Path=ActualHeight,Mode=OneWay}"/><边框 x:Name="插入符号"可见性=折叠"Canvas.Left="0" Canvas.Top="0" Margin="0" 填充="0"宽度=1"高度=16"背景=黑色"><边框.触发器><EventTrigger RoutedEvent="Border.Loaded"><开始故事板><故事板 x:Name="CaretStoryBoard"重复行为="永远"><ColorAnimationUsingKeyFramesStoryboard.TargetProperty="Background.Color"持续时间="0:0:0:1"FillBehavior="HoldEnd"><ColorAnimationUsingKeyFrames.KeyFrames > </ColorAnimationUsingKeyFrames.KeyFrames></ColorAnimationUsingKeyFrames></故事板></BeginStoryboard></事件触发器></Border.Triggers></边界></画布></控制模板></Setter.Value></二传手><EventSetter Event="SelectionChanged" 处理程序="CustomTextBox_SelectionChanged"/><EventSetter Event="GotFocus" 处理程序="CustomTextBox_GotFocus"/><EventSetter Event="LostFocus" 处理程序="CustomTextBox_LostFocus"/></风格>
事件: 插入符号位置
void CustomTextBox_LostFocus(object sender, RoutedEventArgs e){var Caret = FindChild(作为 DependencyObject 的发送者,Caret");Caret.Visibility = Visibility.Collapsed;}void CustomTextBox_GotFocus(对象发送者,RoutedEventArgs e){var Caret = FindChild(作为 DependencyObject 的发送者,Caret");Caret.Visibility = Visibility.Visible;}void CustomTextBox_SelectionChanged(对象发送者,RoutedEventArgs e){var CustomTextBox = FindChild(sender as DependencyObject, "Box");var caretLocation = CustomTextBox.GetRectFromCharacterIndex(CustomTextBox.CaretIndex).Location;var Caret = FindChild(作为 DependencyObject 的发送者,Caret");if (!double.IsInfinity(caretLocation.X)){Canvas.SetLeft(Caret, caretLocation.X);}if (!double.IsInfinity(caretLocation.Y)){Canvas.SetTop(Caret, caretLocation.Y);}}
Helper 方法: To Get Visual Child
public static T FindChild(DependencyObject parent, string childName)其中 T :依赖对象{//确认 parent 和 childName 有效.如果(父 == null)返回 null;T foundChild = null;int childrenCount = VisualTreeHelper.GetChildrenCount(parent);for (int i = 0; i < childrenCount; i++){var child = VisualTreeHelper.GetChild(parent, i);//如果孩子不是请求的孩子类型 childT childType = child as T;if (childType == null){//递归向下钻取树foundChild = FindChild(child, childName);//如果找到孩子,就中断,这样我们就不会覆盖找到的孩子.if (foundChild != null) 中断;}否则 if (!string.IsNullOrEmpty(childName)){var frameworkElement = child as FrameworkElement;//如果孩子的名字被设置为搜索if (frameworkElement != null && frameworkElement.Name == childName){//如果孩子的名字是请求的名字foundChild = (T)child;休息;}}别的{//找到子元素.foundChild = (T)child;休息;}}返回找到的孩子;}
<块引用>
只需在您的代码中添加 Style
/metods
并设置 Style
为了TextBoxes
随心所欲地查看结果.正如我创建的我自己没有对实际 Caret 符号
进行任何实际测量,您可能会看到某种程度的浅色阴影.请调整外观和感觉需要.
I have a textbox inside a viewbox. When I try to resize the window, the textbox size and font size getting scaled, but if I try to focus the textbox and try move the cursor inside the textbox using keyboard, sometimes the cursor is getting disappeared. Is there a way to show the cursor always? Refer the below code which a TextBox inside ViewBox.
<Window x:Class="Resolution_Learning.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Viewbox Stretch="Uniform">
<Grid Width="2560" Height="1440" >
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Hello"/>
<TextBox Grid.Row="0" Grid.Column="1"></TextBox>
<Label Grid.Row="0" Grid.Column="2" Content="Hello"/>
<TextBox Grid.Row="0" Grid.Column="3"/>
<Label Grid.Row="1" Grid.Column="0" Content="Hello"/>
<TextBox Grid.Row="1" Grid.Column="1"/>
<Label Grid.Row="1" Grid.Column="2" Content="Hello"/>
<TextBox Grid.Row="1" Grid.Column="3"/>
</Grid>
</Viewbox>
It's a BUG in WPF. I achieved this by creating my own style for TextBox
Caret
.
XAML: Style for TextBox
<Style TargetType="{x:Type TextBox}" x:Key="CaretStyle" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Canvas>
<TextBox x:Name="Box" CaretBrush="Transparent" Width="{Binding RelativeSource={RelativeSource AncestorType=TextBox},Path=ActualWidth,Mode=OneWay}"
Height="{Binding RelativeSource={RelativeSource AncestorType=TextBox},Path=ActualHeight,Mode=OneWay}"/>
<Border x:Name="Caret"
Visibility="Collapsed"
Canvas.Left="0" Canvas.Top="0" Margin="0" Padding="0"
Width="1" Height="16" Background="Black">
<Border.Triggers>
<EventTrigger RoutedEvent="Border.Loaded">
<BeginStoryboard>
<Storyboard x:Name="CaretStoryBoard"
RepeatBehavior="Forever">
<ColorAnimationUsingKeyFrames
Storyboard.TargetProperty="Background.Color"
Duration="0:0:0:1"
FillBehavior="HoldEnd">
<ColorAnimationUsingKeyFrames.KeyFrames >
<DiscreteColorKeyFrame KeyTime="0:0:0.750"
Value="Transparent" />
<DiscreteColorKeyFrame KeyTime="0:0:0.000"
Value="Black"/>
</ColorAnimationUsingKeyFrames.KeyFrames>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Border.Triggers>
</Border>
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
<EventSetter Event="SelectionChanged" Handler="CustomTextBox_SelectionChanged"/>
<EventSetter Event="GotFocus" Handler="CustomTextBox_GotFocus" />
<EventSetter Event="LostFocus" Handler="CustomTextBox_LostFocus" />
</Style>
EVENTS: For Caret Position
void CustomTextBox_LostFocus(object sender, RoutedEventArgs e)
{
var Caret = FindChild<Border>(sender as DependencyObject, "Caret");
Caret.Visibility = Visibility.Collapsed;
}
void CustomTextBox_GotFocus(object sender, RoutedEventArgs e)
{
var Caret = FindChild<Border>(sender as DependencyObject, "Caret");
Caret.Visibility = Visibility.Visible;
}
void CustomTextBox_SelectionChanged(object sender, RoutedEventArgs e)
{
var CustomTextBox = FindChild<TextBox>(sender as DependencyObject, "Box");
var caretLocation = CustomTextBox.GetRectFromCharacterIndex(CustomTextBox.CaretIndex).Location;
var Caret = FindChild<Border>(sender as DependencyObject, "Caret");
if (!double.IsInfinity(caretLocation.X))
{
Canvas.SetLeft(Caret, caretLocation.X);
}
if (!double.IsInfinity(caretLocation.Y))
{
Canvas.SetTop(Caret, caretLocation.Y);
}
}
Helper Method: To Get Visual Child
public static T FindChild<T>(DependencyObject parent, string childName)
where T : DependencyObject
{
// Confirm parent and childName are valid.
if (parent == null) return null;
T foundChild = null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
// If the child is not of the request child type child
T childType = child as T;
if (childType == null)
{
// recursively drill down the tree
foundChild = FindChild<T>(child, childName);
// If the child is found, break so we do not overwrite the found child.
if (foundChild != null) break;
}
else if (!string.IsNullOrEmpty(childName))
{
var frameworkElement = child as FrameworkElement;
// If the child's name is set for search
if (frameworkElement != null && frameworkElement.Name == childName)
{
// if the child's name is of the request name
foundChild = (T)child;
break;
}
}
else
{
// child element found.
foundChild = (T)child;
break;
}
}
return foundChild;
}
Just add above
Style
/metods
in your code and setStyle
forTextBoxes
wherever you want and see the result. As I have created this myself without any actual measurement of actualCaret symbol
, you may see a light shadow at some scale. please adjust the look & feel as needed.
这篇关于ViewBox 内的 WPF 文本框在调整大小时丢失光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!