问题描述
silverlight Beta 2.0 中TetBox输入汉字,除MS自己的输入法,其它所有输入法都会出现输入的东西会在TextBox中重复一次的现像,google,Baidu了一下,大家说好像是silverlight自己的一个BUG,可能会在Repleass的时候修改。
解决办法:
- /************************************************************************/
- /*
- 作者:覃小春
- 时间:20080826
- 说明:解决silverlightBeta2中TextBox中文输入问题
- * blog:blog.csdn.net/colijian
- */
- /************************************************************************/
- using System.Windows;
- using System.Windows.Controls;
- namespace TextBoxEx
- {
- public class TextBoxEx:TextBox
- {
- #region 属性
- private string _OldText = "";
- private int _RecSelectStart = 0;
- private int _RecSelectLength = 0;
- #endregion
- public TextBoxEx()
- {
- TextChanged += new TextChangedEventHandler(TextBoxEx_TextChanged);
- SelectionChanged += new RoutedEventHandler(TextBoxEx_SelectionChanged);
- }
- void TextBoxEx_SelectionChanged(object sender, RoutedEventArgs e)
- {
- TextBox _sender = sender as TextBox;
- if (_sender == null)
- return;
- if (_sender.SelectionLength > 0)
- {
- //recode user select position
- _RecSelectLength = _sender.SelectionLength;
- _RecSelectStart = _sender.SelectionStart;
- }
- else
- {
- _RecSelectLength = 0;
- }
- }
- void TextBoxEx_TextChanged(object sender, TextChangedEventArgs e)
- {
- TextBox _sender = sender as TextBox;
- if (_sender == null)
- return;
- string textIfnor = _sender.Text;
- #region 除去先中部份
- if (_RecSelectLength != 0)
- {
- _OldText = _OldText.Substring(0, _RecSelectStart) + _OldText.Substring(_RecSelectStart + _RecSelectLength, _OldText.Length - _RecSelectStart - _RecSelectLength);
- _RecSelectLength = 0;
- }
- #endregion
- int LengthAdd = textIfnor.Length - _OldText.Length;
- if (LengthAdd <= 0)
- {
- _OldText = _sender.Text;
- //这种情况是删除数据
- return;
- }
- else if (LengthAdd % 2 == 0)
- {
- //如果当前是成双的情况下
- //得到当前字符串
- string AddInfor = textIfnor.Substring(_sender.SelectionStart - LengthAdd, LengthAdd);
- if (!AddInfor.Substring(0, AddInfor.Length / 2).Equals(AddInfor.Substring(AddInfor.Length / 2)))
- {
- _OldText = _sender.Text;
- return;
- }
- //得到实际新增值
- AddInfor = AddInfor.Substring(0, AddInfor.Length / 2);
- //得到实际理论值
- string DealText = textIfnor.Substring(0, _sender.SelectionStart - LengthAdd) + AddInfor + textIfnor.Substring(_sender.SelectionStart, textIfnor.Length - _sender.SelectionStart);
- int RecodeselectSTart = _sender.SelectionStart - LengthAdd / 2;
- _sender.SelectionStart = 0;
- _sender.Text = DealText;
- _sender.SelectionStart = RecodeselectSTart;
- _OldText = DealText;
- }
- else
- {
- _OldText = _sender.Text;
- }
- }
- }
- }
使用:
- <UserControl x:Class="MutilTextBox.Page"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:CT="clr-namespace:TextBoxEx;assembly=TextBoxEx"
- Width="400" Height="300">
- <Grid x:Name="LayoutRoot" Background="White">
- <Grid.RowDeFinitions>
- <RowDeFinition Height="50"></RowDeFinition>
- <RowDeFinition Height="50"></RowDeFinition>
- <RowDeFinition Height="50"></RowDeFinition>
- <RowDeFinition Height="50"></RowDeFinition>
- </Grid.RowDeFinitions>
- <TextBox x:Name="FirstTextBox" Text="first" Grid.Row="0" TextChanged="FirstTextBox_TextChanged"></TextBox>
- <CT:TextBoxEx x:Name="SecondTextBox" Grid.Row="1"></CT:TextBoxEx>
- <CT:TextBoxEx x:Name="ThreeTextBox" Grid.Row="2"></CT:TextBoxEx>
- <TextBox x:Name="Four" Grid.Row="3" ></TextBox>
- </Grid>
- </UserControl>
注意:要先加入名称空间,具体的值是:
clr-namespace:名称空间全名;assembly=程序集名称
若发此控件有问题,或是不足,请给我留言
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。