有时当我使用文本框时,无法一直向下滚动以查看最后的单词。
我已经包含了一个示例,其中包含三个内容相同但宽度不同的文本框。左边的那个没有显示所有单词。
通过选择它移动到文本框的末尾,然后按'ctrl'+'end'。当我为左边的文本框('_tb1')执行此操作时,我看不到光标,我看不到最后的单词。似乎光标和文字在文本框的“下方”。我可以标记和复制未显示的文本。最后一个字应该是“si +”,见下面的代码。我已经确认文本框的“文本”属性包含所有文本。
到目前为止,这只发生在我使用'TextWrapping =“Wrap”'和某些宽度时。
有关如何修复它的任何建议吗?
<UserControl x:Class="Silverlight4TextBoxProblem.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid Width="500" Height="150">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBox Name="_tb1" Grid.Column="0" TextWrapping="Wrap"
FontFamily="Arial" FontSize="12"
VerticalScrollBarVisibility="Visible"
Width="100"/>
<TextBox Name="_tb2" Grid.Column="1" TextWrapping="Wrap"
FontFamily="Arial" FontSize="12"
VerticalScrollBarVisibility="Visible"
Width="75"/>
<TextBox Name="_tb3" Grid.Column="2" TextWrapping="Wrap"
FontFamily="Arial" FontSize="12"
VerticalScrollBarVisibility="Visible"
Width="150"/>
<Button Grid.Column="3" Click="ButtonClick" Content="Assert _tb1"/>
</Grid>
</UserControl>
代码隐藏
public partial class MainPage : UserControl
{
private readonly string ErrorText = @"Lorem ipsum dolor sit amet+++," + Environment.NewLine
+ "consectetur adipisicing elit, sed do eiusmod" + Environment.NewLine
+ "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam," + Environment.NewLine
+ "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo" + Environment.NewLine
+ "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse" + Environment.NewLine
+ "cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non" + Environment.NewLine
+ "proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem" + Environment.NewLine
+ "ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor" + Environment.NewLine
+ "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis" + Environment.NewLine
+ "nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat." + Environment.NewLine
+ "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu" + Environment.NewLine
+ "fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in" + Environment.NewLine
+ "culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit" + Environment.NewLine
+ "amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore" + Environment.NewLine
+ "et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation" + Environment.NewLine
+ "ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor" + Environment.NewLine
+ "in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla" + Environment.NewLine
+ "pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui" + Environment.NewLine
+ "officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet," + Environment.NewLine
+ "consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et" + Environment.NewLine
+ "dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco" + Environment.NewLine
+ "laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in" + Environment.NewLine
+ "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur." + Environment.NewLine
+ "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia" + Environment.NewLine
+ "deserunt mollit anim id est laborum. Lorem ipsum dolor si+";
public MainPage()
{
InitializeComponent();
_tb1.Text = ErrorText;
_tb2.Text = ErrorText;
_tb3.Text = ErrorText;
}
private void ButtonClick(object sender, RoutedEventArgs e)
{
MessageBox.Show(_tb1.Text.Last().ToString());
}
}
答案 0 :(得分:1)
我努力找到问题的解决方法,但找不到。我试着搞乱模板,100%的时间没有任何作用。
我们应该向Microsoft报告该问题。
答案 1 :(得分:1)
所以我有完全相同的问题。根据您的要求,可以使用RichTextBox来解决此问题。作为奖励,似乎你可以更快地滚动。
但是,RTB没有要绑定的text属性或max length属性。我通过将RTB继承到CustomTextBox来解决这个问题。
如果您连接到事件contentchanged
public class CustomTextBox : RichTextBox
{
public CustomTextBox()
{
ContentChanged += HandleContentChanged;
}
...
然后
private void HandleContentChanged(object sender, ContentChangedEventArgs e)
{
var text = GetContent(out blocks);
Text = text;
}
并添加TextProperty以绑定到
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(CustomTextBox), new PropertyMetadata(string.Empty, HandleTextChanged));
private static void HandleTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var customTextBox = (CustomTextBox)d;
var newValue = (string)e.NewValue;
//This method is only run when set from codebehind
UpdateOnTextChange(customTextBox, newValue);
}
private static void UpdateOnTextChange(CustomTextBox customTextBox, string newValue)
{
int blocks;
if (customTextBox.GetContent(out blocks) != newValue)
{
var binding = customTextBox.GetBindingExpression(TextProperty);
customTextBox.Blocks.Clear();
var value = EnsureString(newValue);
customTextBox.Selection.Text = value;
if (customTextBox.HasTextChanges)
{
customTextBox.HasTextChanges = false;
//reset binding...
if (binding != null)
{
customTextBox.SetBinding(TextProperty, binding.ParentBinding);
}
}
var bindingExpression = customTextBox.GetBindingExpression(TextProperty);
if (bindingExpression != null)
bindingExpression.UpdateSource();
}
}
因为您希望能够将CustomTextBox的内容作为字符串获取:
public string GetContent(out int blocks)
{
var builder = new StringBuilder();
blocks = 0;
foreach (var run in Blocks.OfType<Paragraph>().Select(paragraph => paragraph.Inlines.FirstOrDefault()).OfType<Run>())
{
blocks++;
builder.Append(run.Text);
builder.Append(Environment.NewLine);
}
var content = builder.ToString();
if (content.EndsWith(Environment.NewLine, StringComparison.CurrentCulture))
content = content.Substring(0, content.Length - 2);
return content ?? string.Empty;
}