enhance: add HighlightTextBrush prop for HighlightTextBlock

This commit is contained in:
NaBian 2021-12-23 23:06:23 +08:00
parent 12c6c39424
commit 534f1ef961
6 changed files with 34 additions and 9 deletions

View File

@ -1306,6 +1306,10 @@
<Style x:Key="TextBlockBoldBaseStyle" BasedOn="{StaticResource TextBlockBaseStyle}" TargetType="TextBlock">
<Setter Property="FontWeight" Value="Bold" />
</Style>
<Style x:Key="HighlightTextBlockBaseStyle" TargetType="hc:HighlightTextBlock">
<Setter Property="HighlightBrush" Value="{DynamicResource PrimaryBrush}" />
<Setter Property="HighlightTextBrush" Value="{DynamicResource TextIconBrush}" />
</Style>
<Style x:Key="LabelBaseStyle" TargetType="Label">
<Setter Property="Foreground" Value="{DynamicResource TextIconBrush}" />
<Setter Property="Background" Value="{DynamicResource RegionBrush}" />
@ -3280,6 +3284,7 @@
<Setter Property="FontSize" Value="{StaticResource SubHeadFontSize}" />
</Style>
<Style x:Key="TextBlockDefaultBold" BasedOn="{StaticResource TextBlockBoldBaseStyle}" TargetType="TextBlock" />
<Style BasedOn="{StaticResource HighlightTextBlockBaseStyle}" TargetType="hc:HighlightTextBlock" />
<Style BasedOn="{StaticResource LabelBaseStyle}" TargetType="Label">
<Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}" />
<Setter Property="BorderThickness" Value="1" />

View File

@ -52,7 +52,7 @@
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Source="{Binding ImageName}"/>
<hc:HighlightTextBlock Margin="6,0,0,0" SourceText="{ex:Lang Key={Binding Name}}" QueriesText="{Binding QueriesText}" HighlightBrush="#f06632" />
<hc:HighlightTextBlock Margin="6,0,0,0" SourceText="{ex:Lang Key={Binding Name}}" QueriesText="{Binding QueriesText}"/>
<Path Visibility="{Binding IsNew,Converter={StaticResource Boolean2VisibilityConverter}}" VerticalAlignment="Center" Data="{StaticResource NewGeometry}" Stretch="Uniform" Margin="10,0,0,0" Width="30" Height="14" Fill="{DynamicResource DangerBrush}"/>
</StackPanel>
</DataTemplate>

View File

@ -21,8 +21,10 @@ namespace HandyControl.Controls
new PropertyMetadata(null, OnQueriesTextChanged));
public static readonly DependencyProperty HighlightBrushProperty =
DependencyProperty.Register("HighlightBrush", typeof(Brush), typeof(HighlightTextBlock),
new PropertyMetadata(Brushes.Yellow));
DependencyProperty.Register("HighlightBrush", typeof(Brush), typeof(HighlightTextBlock));
public static readonly DependencyProperty HighlightTextBrushProperty =
DependencyProperty.Register("HighlightTextBrush", typeof(Brush), typeof(HighlightTextBlock));
private static void OnSourceTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) =>
((HighlightTextBlock) d).RefreshInlines();
@ -64,6 +66,12 @@ namespace HandyControl.Controls
set => SetValue(HighlightBrushProperty, value);
}
public Brush HighlightTextBrush
{
get => (Brush) GetValue(HighlightTextBrushProperty);
set => SetValue(HighlightTextBrushProperty, value);
}
private void RefreshInlines()
{
Inlines.Clear();
@ -97,12 +105,10 @@ namespace HandyControl.Controls
private Run GetHighlightRun(string highlightText)
{
var run = new Run(highlightText);
run.SetBinding(TextElement.BackgroundProperty, new Binding
{
Source = this,
Path = new PropertyPath(nameof(HighlightBrush)),
Mode = BindingMode.OneWay
});
run.SetBinding(TextElement.BackgroundProperty, new Binding(nameof(HighlightBrush)) { Source = this });
run.SetBinding(TextElement.ForegroundProperty, new Binding(nameof(HighlightTextBrush)) { Source = this });
return run;
}

View File

@ -1,4 +1,5 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:hc="clr-namespace:HandyControl.Controls"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="TextBlockBaseStyle" TargetType="TextBlock">
@ -11,4 +12,9 @@
<Setter Property="FontWeight" Value="Bold"/>
</Style>
<Style x:Key="HighlightTextBlockBaseStyle" TargetType="hc:HighlightTextBlock">
<Setter Property="HighlightBrush" Value="{DynamicResource PrimaryBrush}"/>
<Setter Property="HighlightTextBrush" Value="{DynamicResource TextIconBrush}"/>
</Style>
</ResourceDictionary>

View File

@ -1,4 +1,5 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:hc="clr-namespace:HandyControl.Controls"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!--默认样式-->
@ -49,4 +50,6 @@
</Style>
<Style x:Key="TextBlockDefaultBold" BasedOn="{StaticResource TextBlockBoldBaseStyle}" TargetType="TextBlock"/>
<Style BasedOn="{StaticResource HighlightTextBlockBaseStyle}" TargetType="hc:HighlightTextBlock"/>
</ResourceDictionary>

View File

@ -1306,6 +1306,10 @@
<Style x:Key="TextBlockBoldBaseStyle" BasedOn="{StaticResource TextBlockBaseStyle}" TargetType="TextBlock">
<Setter Property="FontWeight" Value="Bold" />
</Style>
<Style x:Key="HighlightTextBlockBaseStyle" TargetType="hc:HighlightTextBlock">
<Setter Property="HighlightBrush" Value="{DynamicResource PrimaryBrush}" />
<Setter Property="HighlightTextBrush" Value="{DynamicResource TextIconBrush}" />
</Style>
<Style x:Key="LabelBaseStyle" TargetType="Label">
<Setter Property="Foreground" Value="{DynamicResource TextIconBrush}" />
<Setter Property="Background" Value="{DynamicResource RegionBrush}" />
@ -3280,6 +3284,7 @@
<Setter Property="FontSize" Value="{StaticResource SubHeadFontSize}" />
</Style>
<Style x:Key="TextBlockDefaultBold" BasedOn="{StaticResource TextBlockBoldBaseStyle}" TargetType="TextBlock" />
<Style BasedOn="{StaticResource HighlightTextBlockBaseStyle}" TargetType="hc:HighlightTextBlock" />
<Style BasedOn="{StaticResource LabelBaseStyle}" TargetType="Label">
<Setter Property="Foreground" Value="{DynamicResource PrimaryTextBrush}" />
<Setter Property="BorderThickness" Value="1" />