ViewBox 中的 WPF 文本框在调整大小时会丢失光标



我在视图框中有一个文本框。当我尝试调整窗口大小时,文本框大小和字体大小会缩放,但是如果我尝试聚焦文本框并尝试使用键盘在文本框内移动光标,有时光标会消失。有没有办法始终显示光标?请参考下面的代码,其中 一个文本框 在视图框中。

<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>

这是

WPF中的一个错误。我通过为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>

事件: 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);
        }
    }

帮助程序方法: 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;
        }

只需在代码中添加上面的 Style/metods 并设置 Style TextBoxes您想要的任何地方并查看结果。正如我所创造的 这本身没有任何实际测量的实际Caret symbol,你可能会看到一定比例的光影。请调整 根据需要的外观和感觉。

我认为这里发生的事情是这样的:

由于 ViewBox 内的所有控件都已重新调整大小,因此由于像素计算,并不总是绘制插入符号。

要使其正常工作,我们必须创建自己的插入符号。我已经在网上搜索了,以防有人已经实现了这个,并找到了d.moncada的这篇文章。

我已经下载了源代码,并使用了CustomCaretTextBox(而不是常规的TextBox)来测试它是否可以解决问题,它确实做到了!

我强烈建议您尝试该解决方案,因为它看起来很优雅,开箱即用,并且可以根据您的需求轻松操作。

祝您编码愉快! :)

我能够通过使文本框的样式拉伸来解决此问题,如下所示:

 <Style x:Name="TextBoxStyleTitle"
           TargetType="TextBox">
        <Setter Property="VerticalAlignment"
                Value="Stretch"></Setter>
        <Setter Property="HorizontalAlignment"
                Value="Stretch"></Setter>
 </Style>

最新更新