所以要么我错过了一些简单的东西,要么这是我长期以来遇到的最奇怪的错误。
这是我的申请:
用户可以通过点击“添加步骤”来添加新的TestStep。左下角的按钮。每个TestStep(在本例中为三个)都是他们自己的自包含UserControls,它们被添加到透明的stackpanel中。双击某个步骤将允许用户进行如下编辑:
到目前为止一切顺利!如果我选择除了面板底部最后一步之外的任何步骤(在这种情况下为#3),这就是它变得奇怪的地方我得到了这个:
除 SelectAll()
功能外,一切正常。
'添加步骤'左边的小箭头按钮允许用户通过向上移动选定的步骤来调整TestSteps的顺序。重新排序时,我删除了所选的步骤,然后在上面插入一个索引,这有效地使它成为"交换位置"使用上面的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>
答案 0 :(得分:0)
希望这有帮助:
RichTextBox.Loaded += delegate
{
Dispatcher.BeginInvoke(
DispatcherPriority.Normal,
new Action(
delegate()
{
Keyboard.Focus(RichTextBox);
RichTextBox.Selection.Select(
RichTextBox.Document.ContentStart,
RichTextBox.Document.ContentEnd);
}));
};
答案 1 :(得分:0)
所以我最终找到了问题。我有一个Keyboard.ClearFocus();
代码行,用于清除所有其他未选择步骤的选择。这确保了如果您选择步骤1然后单击以突出显示步骤2,则步骤1仍然没有插入符号闪烁或选择了文本。不幸的是,根据我循环的方式,它并没有将这一行约束到对象的实例并冒泡到整个应用程序。
因此,在选择步骤2的三个步骤的情况下,它将取消选择步骤1,选择步骤2,并取消选择步骤3,这也将遗憾地取消选择步骤2而不是保留在步骤2中。实例。