文本框有时会缺少光标

[英]Text Box sometimes cursor is missing


I have created a custom water mark text box which is extended from text box. control template for the same is shown below.

我创建了一个自定义水印文本框,从文本框扩展。其控制模板如下所示。

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type controls:WaterMarkTextBox}">
            <ControlTemplate.Resources>
                <Storyboard x:Key="Storyboard1">
                    <ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)"
                                                      Storyboard.TargetName="PART_FieldTextBlock">
                        <SplineThicknessKeyFrame KeyTime="0:0:0.15"
                                                 Value="0,0,10,0" />
                    </ThicknessAnimationUsingKeyFrames>
                </Storyboard>
                <Storyboard x:Key="Storyboard2">
                    <ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)"
                                                      Storyboard.TargetName="PART_FieldTextBlock">
                        <SplineThicknessKeyFrame KeyTime="0:0:0.25"
                                                 Value="0,0,-500,0" />
                    </ThicknessAnimationUsingKeyFrames>
                </Storyboard>
            </ControlTemplate.Resources>
            <Grid x:Name="PART_GridControl"
                  ClipToBounds="True"
                  Height="{TemplateBinding Height}"
                  Width="{TemplateBinding Width}">
                <TextBlock x:Name="PART_PlaceHolderTextBlock"
                           Style="{StaticResource SWMLightTextBlockStyle}"
                           Foreground="#BDBBBB"
                           FontSize="{StaticResource SmallFontSize}"
                           Text="{TemplateBinding PlaceHolderText}"
                           VerticalAlignment="Center"
                           Margin="20,0,10,0" />
                <Border Name="border"
                        CornerRadius="0"
                        Padding="2"
                        BorderThickness="1"
                        BorderBrush="DeepSkyBlue">
                    <ScrollViewer x:Name="PART_ContentHost"/>
                </Border>
                <TextBlock x:Name="PART_FieldTextBlock"
                           HorizontalAlignment="Right"
                           Foreground="#BDBBBB"
                           Margin="0,0,-500,0"
                           Style="{StaticResource SWMLightTextBlockStyle}"
                           FontSize="{StaticResource SmallFontSize}"
                           TextWrapping="Wrap"
                           Text="{TemplateBinding FieldText}"
                           VerticalAlignment="Center" />
            </Grid>
        </ControlTemplate>
    </Setter.Value>
</Setter>

but while typing the textbox enters to a condition where it is having no cursor but we can type into it occurs with a probability of 1/2 chars.I wonder how it happens. Anyone is having idea how it is happening?

但是当输入文本框进入一个没有光标的情况但是我们可以输入它的概率是1/2字符。我想知道它是如何发生的。任何人都知道它是如何发生的?

1 个解决方案

#1


0  

Two things You can do. First One is Overriding SystemParameters Properties Using reflection Like this

你可以做两件事。第一个是覆盖SystemParameters属性使用反射像这样

void LocallyDisableMouseVanish()
    {
        foreach (var field in typeof(SystemParameters).GetFields(BindingFlags.NonPublic|BindingFlags.Static))
            if (field.Name.Contains("mouseVanish"))
                field.SetValue(null, false);
    }

Call this method On Focus Of your Control.

将此方法称为“控制焦点”。

If this is Not working means You can Try something Like this Override the style of the Caret Like this

如果这不起作用意味着你可以试试这样的东西覆盖Caret的风格就像这样

 <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type controls:WaterMarkTextBox}">
                <ControlTemplate.Resources>
                    <Storyboard x:Key="Storyboard1">
                        <ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)"
                                                          Storyboard.TargetName="PART_FieldTextBlock">
                            <SplineThicknessKeyFrame KeyTime="0:0:0.15"
                                                     Value="0,0,10,0" />
                        </ThicknessAnimationUsingKeyFrames>
                    </Storyboard>
                    <Storyboard x:Key="Storyboard2">
                        <ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)"
                                                          Storyboard.TargetName="PART_FieldTextBlock">
                            <SplineThicknessKeyFrame KeyTime="0:0:0.25"
                                                     Value="0,0,-500,0" />
                        </ThicknessAnimationUsingKeyFrames>
                    </Storyboard>
                </ControlTemplate.Resources>
                <Grid x:Name="PART_GridControl"
                      ClipToBounds="True"
                      Height="{TemplateBinding Height}"
                      Width="{TemplateBinding Width}">
                    <TextBlock x:Name="PART_PlaceHolderTextBlock"
                               Style="{StaticResource SWMLightTextBlockStyle}"
                               Foreground="#BDBBBB"
                               FontSize="{StaticResource SmallFontSize}"
                               Text="{TemplateBinding PlaceHolderText}"
                               VerticalAlignment="Center"
                               Margin="20,0,10,0" />
                    <Border Name="border"
                            CornerRadius="0"
                            Padding="2"
                            BorderThickness="1"
                            BorderBrush="DeepSkyBlue">
                        <ScrollViewer x:Name="PART_ContentHost" />
                    </Border>
                    <TextBlock x:Name="PART_FieldTextBlock"
                               HorizontalAlignment="Right"
                               Foreground="#BDBBBB"
                               Margin="0,0,-500,0"
                               Style="{StaticResource SWMLightTextBlockStyle}"
                               FontSize="{StaticResource SmallFontSize}"
                               TextWrapping="Wrap"
                               Text="{TemplateBinding FieldText}"
                               VerticalAlignment="Center" />
                    <Canvas>
                        <Border x:Name="PART_Caret"
                                Visibility="Collapsed"
                                Canvas.Left="0"
                                Canvas.Top="0"
                                Width="5"
                                Height="25"
                                Background="Black"
                                BorderThickness="1">
                            <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>
                </Grid>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="CaretBrush "
            Value="Transparent" />

And in your control code Add this

并在您的控制代码中添加此项

 public override void OnApplyTemplate()
    {
        this.border = this.GetTemplateChild("PART_Caret") as Border;
        base.OnApplyTemplate();
    }

And add this Method

并添加此方法

 private void MoveCustomCaret()
    {
        var caretLocation = this.GetRectFromCharacterIndex(this.CaretIndex).Location;

        if (!double.IsInfinity(caretLocation.X))
        {
            Canvas.SetLeft(border, caretLocation.X);
        }

        if (!double.IsInfinity(caretLocation.Y))
        {
            Canvas.SetTop(border, caretLocation.Y);
        }
    }

And Finally Call This method and set the visibility of the border

最后调用此方法并设置边框的可见性

private void SWMTextBox_GotFocus(object sender, RoutedEventArgs e)
    {
        MoveCustomCaret();
        border.Visibility = Visibility.Visible;
    }

also in TextChangedEvent

也在TextChangedEvent中

private void CustomTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        MoveCustomCaret();
    }

And Hid the Visibility in Lost_Focus_Event

并隐藏了Lost_Focus_Event中的可见性

private void SWMTextBox_LostFocus(object sender, RoutedEventArgs e)
    {
        border.Visibility = Visibility.Hidden;
    }

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2015/08/14/2fbbc9b8d1f2e84576a4e0f5d8f3c147.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号