【发布时间】:2015-04-05 01:49:53
【问题描述】:
所以要么我错过了一些愚蠢的简单的东西,要么这是我很长时间以来遇到的最奇怪的错误。
这是我的申请:
用户可以通过单击左下角的“添加步骤”按钮来添加新的测试步骤。每个 TestStep(在本例中为三个)都是它们自己的独立用户控件,它们被添加到透明的堆栈面板中。双击一个步骤将允许用户像这样进行编辑:
到目前为止一切顺利!这就是奇怪的地方,如果我选择面板底部最后一步以外的任何步骤(在本例中为#3),我会得到:
一切正常除了SelectAll() 功能。
“添加步骤”按钮左侧的小箭头允许用户通过将所选步骤上移一个来调整测试步骤的顺序。重新排序时,我删除选定的步骤,然后在上面插入一个索引,这有效地使它与上面的 TestStep“交换位置”。我指出这一点的原因是SelectAll() 问题仍然存在。无论最后添加的是哪个控件,最底部的一个都有效,其他所有的都无效。
下面是由 UserControl 上的MouseUp 事件触发的代码:
contentBox.IsHitTestVisible = true;
contentBox.Background = new SolidColorBrush(Color.FromRgb(240, 240, 240));
contentBox.IsReadOnly = false;
contentBox.Focus();
contentBox.SelectAll();
此代码存在于每个 UserControl 中,因此contentBox 引用了该特定实例的 RichTextBox。请注意,HitTest、Background 和 IsReadOnly 属性在每个步骤中始终有效。以前我使用以下代码将插入符号设置为文本的末尾:
contentBox.IsHitTestVisible = true;
contentBox.Background = new SolidColorBrush(Color.FromRgb(240, 240, 240));
contentBox.IsReadOnly = false;
//manage caret position
TextPointer newPointer = contentBox.CaretPosition;
newPointer = newPointer.DocumentEnd;
contentBox.CaretPosition = newPointer;
contentBox.Focus();
Keyboard.Focus(contentBox);
使用此代码块,一切正常,表明问题与正确聚焦 contentBox 无关。
任何帮助或建议将不胜感激!在这一点上,我已经没有想法了。
谢谢!
更新
这是 StackPanel 的 XAML:
<ScrollViewer Grid.Column="1" HorizontalAlignment="Stretch" Margin="0,5,5,0" Grid.Row="1" VerticalAlignment="Stretch" >
<StackPanel x:Name="stepPanel" HorizontalAlignment="Stretch" Background="White" />
</ScrollViewer>
这是用户控件的 XAML:
<UserControl x:Name="mainStepControl" x:Class="Test_Script_Writer_2._1.testStep"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
MouseEnter="mainStepControl_MouseEnter"
MouseLeave="mainStepControl_MouseLeave"
MouseUp="mainStepControl_MouseUp"
d:DesignHeight="150" d:DesignWidth="400" Background="White">
<Border x:Name="mainStepBorder" BorderBrush="#FFD9C5B4" BorderThickness="3" CornerRadius="6" Margin="3" Padding="3" Background="#8DA3BD">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="17*"/>
<ColumnDefinition Width="1.5*"/>
</Grid.ColumnDefinitions>
<RichTextBox x:Name="contentBox"
Background="Transparent"
BorderThickness="0"
Margin="0,0,0,0"
Grid.Column="1"
Grid.RowSpan="2"
IsReadOnly="False"
IsHitTestVisible="False"
BorderBrush="{x:Null}"
SelectionBrush="#FF3399FF">
<FlowDocument>
<Paragraph/>
</FlowDocument>
</RichTextBox>
<Label x:Name="indexLabel" Content="Test" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0" FontSize="16" FontWeight="Bold" FontFamily="MV Boli" Foreground="#FF070FBB"/>
<Button Grid.Column="2" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Right" VerticalAlignment="Center" Width="25" FontWeight="Bold" BorderBrush="{x:Null}" Foreground="Red" Height="25" Click="Button_Click" Margin="0,0,3,0" >
<Button.Background>
<ImageBrush ImageSource="Resources/delete (1).png" Stretch="Uniform"/>
</Button.Background>
</Button>
</Grid>
</Border>
</UserControl>
【问题讨论】:
-
您是否使用任何样式进行实时出价?显示 RTB 和堆栈面板的 XAML 或代码
-
@Ganesh 请查看 XAML 的更新问题。我没有使用任何我知道的样式。另外,即使是这样的情况,这不会平等地影响所有实例吗?
-
你能上传你的演示吗?
-
@Neil 我做了一个简单的发布并将文件放入 Dropbox:dropbox.com/sh/fgunifl9tmlr1w5/AAAGDP5UXAbkW1JS_7ThWSiCa?dl=0
标签: c# wpf richtextbox caret