RTB SelectAll 仅处理 StackPanel 中的最后一项
RTB SelectAll only working on last item in StackPanel
所以要么我遗漏了一些非常简单的东西,要么这是我很长时间以来遇到的最奇怪的错误。
这是我的申请:
用户可以通过单击左下角的'Add Step' 按钮来添加一个New TestStep。每个 TestStep(在本例中为三个)都是它们自己的独立用户控件,这些控件被添加到一个透明的堆栈面板中。双击一个步骤将允许用户像这样编辑:
到目前为止一切顺利!这是它变得奇怪的地方,如果我 select 除了面板底部的最后一步以外的任何步骤(在这种情况下为#3),我得到这个:
一切正常 除了 SelectAll()
功能。
'Add Step' 按钮左侧的小箭头允许用户通过将 selected 步骤向上移动来调整测试步骤的顺序。重新排序时,我删除了 selected 步骤,然后在上面插入一个索引,这有效地使它 "swap places" 上面有 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>
希望对您有所帮助:
RichTextBox.Loaded += delegate
{
Dispatcher.BeginInvoke(
DispatcherPriority.Normal,
new Action(
delegate()
{
Keyboard.Focus(RichTextBox);
RichTextBox.Selection.Select(
RichTextBox.Document.ContentStart,
RichTextBox.Document.ContentEnd);
}));
};
所以我最终找到了问题所在。我有一行 Keyboard.ClearFocus();
代码,用于清除所有其他未 select 编辑步骤的 selection。这确保了如果您 select 步骤 1 然后单击以突出显示步骤 2,则步骤 1 不会仍然有插入符号闪烁或文本 selected。不幸的是,根据我执行循环的方式,它没有将此行限制为对象的实例并冒泡到整个应用程序。
所以如果有三个步骤,其中第 2 步应该是 selected,它将 unselect step 1,select step 2,和 unselect 第 3 步,不幸的是,这也会取消 select 第 2 步,而不是留在它的实例中。
所以要么我遗漏了一些非常简单的东西,要么这是我很长时间以来遇到的最奇怪的错误。
这是我的申请:
用户可以通过单击左下角的'Add Step' 按钮来添加一个New TestStep。每个 TestStep(在本例中为三个)都是它们自己的独立用户控件,这些控件被添加到一个透明的堆栈面板中。双击一个步骤将允许用户像这样编辑:
到目前为止一切顺利!这是它变得奇怪的地方,如果我 select 除了面板底部的最后一步以外的任何步骤(在这种情况下为#3),我得到这个:
一切正常 除了 SelectAll()
功能。
'Add Step' 按钮左侧的小箭头允许用户通过将 selected 步骤向上移动来调整测试步骤的顺序。重新排序时,我删除了 selected 步骤,然后在上面插入一个索引,这有效地使它 "swap places" 上面有 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>
希望对您有所帮助:
RichTextBox.Loaded += delegate
{
Dispatcher.BeginInvoke(
DispatcherPriority.Normal,
new Action(
delegate()
{
Keyboard.Focus(RichTextBox);
RichTextBox.Selection.Select(
RichTextBox.Document.ContentStart,
RichTextBox.Document.ContentEnd);
}));
};
所以我最终找到了问题所在。我有一行 Keyboard.ClearFocus();
代码,用于清除所有其他未 select 编辑步骤的 selection。这确保了如果您 select 步骤 1 然后单击以突出显示步骤 2,则步骤 1 不会仍然有插入符号闪烁或文本 selected。不幸的是,根据我执行循环的方式,它没有将此行限制为对象的实例并冒泡到整个应用程序。
所以如果有三个步骤,其中第 2 步应该是 selected,它将 unselect step 1,select step 2,和 unselect 第 3 步,不幸的是,这也会取消 select 第 2 步,而不是留在它的实例中。