0
有没有什么办法可以做到WPF中TextBox.HideSelection = false的等价物? 下面的代码尽可能接近我可以得到它 - 你需要通过字段“选项卡”来让它们显示出来。 当按下按钮时,我需要选择至少全部显示。 (我的实际应用是绑定到selectionstart /长度设置) (另:任何其他控件,允许范围突出显示也可以!) 感谢您的帮助!TextBox HideSelection in XAML
<Window x:Class="selectionbrush.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="IsInactiveSelectionHighlightEnabled" Value="True"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="Margin" Value="5"/>
<Setter Property="SelectionBrush" Value="Green"/>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="SelectionBrush" Value="Red"/>
</MultiTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<Button Content="set selections" Click="Button_Click" Margin="5" MaxWidth="100" HorizontalAlignment="Left" Padding="5,0,5,0"/>
<TextBox x:Name="tb1" SelectionStart="1" SelectionLength="8" Text="Lorem ipsum dolor sit amet, agam dolore mediocritatem eu qui"/>
<TextBox x:Name="tb2" SelectionStart="5" SelectionLength="3" Text="Lorem ipsum dolor sit amet, agam dolore mediocritatem eu qui"/>
<TextBox x:Name="tb3" SelectionStart="9" SelectionLength="12" Text="Lorem ipsum dolor sit amet, agam dolore mediocritatem eu qui"/>
<TextBox x:Name="tb4" SelectionStart="4" SelectionLength="9" Text="Lorem ipsum dolor sit amet, agam dolore mediocritatem eu qui"/>
<TextBox x:Name="tb5" SelectionStart="11" SelectionLength="4" Text="Lorem ipsum dolor sit amet, agam dolore mediocritatem eu qui"/>
</StackPanel>
有了这个代码隐藏:
private void Button_Click(object sender, RoutedEventArgs e)
{
tb1.SelectionStart = 6; tb1.SelectionLength = 5;
tb2.SelectionStart = 17; tb2.SelectionLength = 7;
tb3.SelectionStart = 8; tb3.SelectionLength = 8;
tb4.SelectionStart = 12; tb4.SelectionLength = 3;
tb5.SelectionStart = 14; tb5.SelectionLength = 9;
}
这似乎是不可能与文本框 - 我将使用RichTextBox代替... – DanW