2015-08-14 93 views
7

我已經創建了一個從文本框擴展的自定義水印文本框。控制模板的相同如下所示。文本框有時會丟失光標

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

但同時打字文本框進入到其被不具有光標的狀態,但到它具有1/2 chars.I的概率發生,我們可以鍵入不知道它是如何發生的。任何人都知道它是如何發生的?

回答

0

兩件事你可以做。第一個是重寫SystemParameters屬性使用反射像這樣

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

調用此方法可在聚焦你的控制。

如果這不是工作意味着你可以嘗試這樣的事情重寫符的風格像這樣

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

而在你的控制代碼添加此

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

並添加此方法

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此方法並設置邊框的可見性

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

TextChangedEvent

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

和HID的能見度Lost_Focus_Event

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