在覆盖模式下,是否可以在silverlight中更改TextBox的插入符宽度?

时间:2015-08-19 10:52:36

标签: silverlight caret

我在Silverlight 4中有两种模式的TextBox:插入覆盖

任何人都可以帮助我吗?如果我按下覆盖模式,我想让插入符号闪烁更大。

我已经习惯了CaretBrush,但它只能改变插入符号的颜色。 非常感谢您有建议或示例代码。

1 个答案:

答案 0 :(得分:0)

当时有人可以提供帮助,我为自己尝试了这个解决方案,这很复杂,但我希望有人可以使用我的代码并自己定制。如果您有这种情况,有人可以获取此代码进行尝试 MainPage.xaml代码:

<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.micros`enter code here`oft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <Grid  Name="LayoutRoot">
        <Canvas x:Name="canvasDataView" VerticalAlignment="Top"  Background="White" HorizontalAlignment="Left">
            <TextBox HorizontalAlignment="Left" Canvas.Left="50" VerticalAlignment="Top" x:Name="positionTextBox" Canvas.ZIndex="2" Text="position" Canvas.Top="25" Visibility="Collapsed" Width="100" Height="25"></TextBox>
            <TextBox x:Name="textBox1" Text="asdfasdfasd asda asdf as adfasdfads adf asdf adfasdf" SelectionForeground="AliceBlue" KeyDown="textBox1_KeyDown" KeyUp="textBox1_KeyUp" TextWrapping="NoWrap" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Visible" AcceptsReturn="True" Height="25" Width="500" />
            <TextBox x:Name="shadowTextBox" Canvas.ZIndex="-1" Canvas.Left="350" Height="20" Width="20" HorizontalScrollBarVisibility="Visible" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" Visibility="Visible" TextWrapping="NoWrap"/>
        </Canvas>        

    </Grid>
</UserControl>

背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Expression.Interactivity;
using System.Windows.Controls.Primitives;
namespace SilverlightApplication1
{
  public partial class MainPage : UserControl
  {    
    Duration duration = new Duration(TimeSpan.FromSeconds(0.5));
    Storyboard storybroad = new Storyboard();
    Rectangle myRectangle = null;
    ScrollViewer sv;
    public MainPage()
    {
      InitializeComponent();         
      textBox1.KeyUp += new KeyEventHandler(textBox1_KeyUp);      
      textBox1.TextChanged += new TextChangedEventHandler(textBox1_TextChanged);
      textBox1.CaretBrush = new SolidColorBrush(Colors.Transparent);
    }
    void textBox1_TextChanged(object sender, TextChangedEventArgs e)
    {
      shadowTextBox.Text = textBox1.Text;
      shadowTextBox.SelectionStart = textBox1.SelectionStart;
      Point p = getCaretPosition(textBox1, shadowTextBox);
      MoveCaret(p);
    }
    void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
      shadowTextBox.Text = textBox1.Text;
      shadowTextBox.SelectionStart = textBox1.SelectionStart;
    }
    void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
      shadowTextBox.Text = textBox1.Text;
      shadowTextBox.SelectionStart = textBox1.SelectionStart;
      // get caret position
      Point p = getCaretPosition(textBox1,shadowTextBox);
      MoveCaret(p);
    }    
    private Point getCaretPosition(TextBox textBox, TextBox shadowTextBox)
    {
      Point _point = new Point();
      // get main textbox's scroll offset, if any
      getScrollBar(textBox);
      double initVerticalOffset = sv.VerticalOffset;
      double initHorizontalOffset = sv.HorizontalOffset;
      // get shadow box scroll offset
      getScrollBar(shadowTextBox);
      double vOffset = sv.VerticalOffset;
      double hOffset = sv.HorizontalOffset;
      // caret position is scroll offset of shadaw minus scroll offset of main (if any)
      _point.Y = vOffset - initVerticalOffset;
      _point.X = hOffset - initHorizontalOffset;
      return _point;
    }
    private void getScrollBar(UIElement src)
    {
        // walk visual tree for this object until we get the scrollviewer
        if (src.GetType().ToString() == "System.Windows.Controls.ScrollViewer")
            sv = src as ScrollViewer;
        else
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(src); i++)
            {
                UIElement elem = (UIElement)VisualTreeHelper.GetChild(src, i);
                getScrollBar(elem);
            }
        }
    }
    void MoveCaret(Point point)
    {
      if (storybroad == null)
        storybroad = new Storyboard();
      storybroad.Stop();
      if (point != new Point(0, 0))
      {
        if (myRectangle != null && canvasDataView.Children.Contains(myRectangle))
        {
          canvasDataView.Children.Remove(myRectangle);
        }
        myRectangle = new Rectangle();
        myRectangle.SetValue(Canvas.TopProperty, point.Y);
        myRectangle.SetValue(Canvas.LeftProperty, point.X);
        myRectangle.Width = 5;
        myRectangle.Height = 16;
        myRectangle.Fill = new SolidColorBrush();
        storybroad.BeginTime = new TimeSpan(0, 0, 0, 0, 0);
        storybroad.RepeatBehavior = RepeatBehavior.Forever;
        ColorAnimation color = new ColorAnimation();
        canvasDataView.Children.Add(myRectangle);
        color.From = Colors.Transparent;
        color.To = Colors.Green;
        color.Duration = duration;
        storybroad.Children.Add(color);
        Storyboard.SetTarget(color, myRectangle);
        Storyboard.SetTargetProperty(color, new PropertyPath("(Fill).(SolidColorBrush.Color)"));        
        storybroad.Begin();
      }
    }   
  }
}
相关问题