2015-08-19 28 views

回答

0

當時看到有人可以幫忙,我爲自己嘗試了這個解決方案,它有點複雜,但我希望有人可以使用我的代碼並自定義自己。

<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(); 
     } 
    } 
    } 
} 
如果你有這種情況 MainPage.xaml中的代碼有人能得到這個代碼試圖