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