Skip to content

Commit

Permalink
Merge pull request #391 from reduckted/feature/rich-text-box-theme
Browse files Browse the repository at this point in the history
Added Visual Studio theme styles for RichTextBox
  • Loading branch information
madskristensen authored Nov 23, 2022
2 parents 688428c + 40d9054 commit 2020952
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,32 @@
</local:ThemedControl.Disabled>
</local:ThemedControl>

<local:ThemedControl Label="RichTextBox:">
<local:ThemedControl.Enabled>
<RichTextBox>
<FlowDocument>
<Paragraph>
Rich
<Bold>Text</Bold>
<Italic>Box</Italic>
</Paragraph>
</FlowDocument>
</RichTextBox>
</local:ThemedControl.Enabled>

<local:ThemedControl.Disabled>
<RichTextBox IsEnabled="False">
<FlowDocument>
<Paragraph>
Rich
<Bold>Text</Bold>
<Italic>Box</Italic>
</Paragraph>
</FlowDocument>
</RichTextBox>
</local:ThemedControl.Disabled>
</local:ThemedControl>

<local:ThemedControl Label="PasswordBox:">
<local:ThemedControl.Enabled>
<PasswordBox Password="Password" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,58 @@
</Style.Triggers>
</Style>

<ControlTemplate x:Key="{x:Static local:ToolkitResourceKeys.RichTextBoxControlTemplateKey}" TargetType="{x:Type RichTextBox}">
<!--
The default template for a RichTextBox defines a trigger for IsMouseOver that changes the
border brush. To get our style triggers to apply, we need to override the template.
-->
<Border
x:Name="border"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
>

<ScrollViewer
x:Name="PART_ContentHost"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
/>
</Border>
</ControlTemplate>

<Style x:Key="{x:Static local:ToolkitResourceKeys.RichTextBoxStyleKey}" TargetType="RichTextBox">
<Setter Property="Padding" Value="{StaticResource {x:Static local:ToolkitResourceKeys.InputPaddingKey}}" />
<Setter Property="Background" Value="{DynamicResource {x:Static local:InternalResourceKeys.CommonControlsColors_TextBoxBackgroundBrushKey}}" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static local:InternalResourceKeys.CommonControlsColors_TextBoxTextBrushKey}}" />
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static local:InternalResourceKeys.CommonControlsColors_TextBoxBorderBrushKey}}" />

<Setter Property="Template" Value="{StaticResource {x:Static local:ToolkitResourceKeys.RichTextBoxControlTemplateKey}}"/>

<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static local:InternalResourceKeys.CommonControlsColors_TextBoxBackgroundBrushKey}}" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static local:InternalResourceKeys.CommonControlsColors_TextBoxTextBrushKey}}" />
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static local:InternalResourceKeys.CommonControlsColors_TextBoxBorderBrushKey}}" />
</Trigger>

<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static local:InternalResourceKeys.CommonControlsColors_TextBoxBackgroundFocusedBrushKey}}" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static local:InternalResourceKeys.CommonControlsColors_TextBoxTextFocusedBrushKey}}" />
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static local:InternalResourceKeys.CommonControlsColors_TextBoxBorderFocusedBrushKey}}" />
</Trigger>

<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="{DynamicResource {x:Static local:InternalResourceKeys.CommonControlsColors_TextBoxBackgroundDisabledBrushKey}}" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static local:InternalResourceKeys.CommonControlsColors_TextBoxTextDisabledBrushKey}}" />
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static local:InternalResourceKeys.CommonControlsColors_TextBoxBorderDisabledBrushKey}}" />
</Trigger>
</Style.Triggers>
</Style>

<!-- Default styles. -->
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Static local:ToolkitResourceKeys.TextBoxStyleKey}}" />
<Style TargetType="ComboBox" BasedOn="{StaticResource {x:Static local:ToolkitResourceKeys.ComboBoxStyleKey}}" />
<Style TargetType="PasswordBox" BasedOn="{StaticResource {x:Static local:ToolkitResourceKeys.PasswordBoxStyleKey}}" />
<Style TargetType="RichTextBox" BasedOn="{StaticResource {x:Static local:ToolkitResourceKeys.RichTextBoxStyleKey}}" />
</ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ public static class ToolkitResourceKeys
/// <summary>Gets the key that defines the resource for the <see cref="ControlTemplate"/> of a Visual Studio-themed <see cref="PasswordBox"/> style.</summary>
public static object PasswordBoxControlTemplateKey { get; } = _prefix + nameof(PasswordBoxControlTemplateKey);

/// <summary>Gets the key that defines the resource for a Visual Studio-themed <see cref="RichTextBox"/> style.</summary>
public static object RichTextBoxStyleKey { get; } = _prefix + nameof(RichTextBoxStyleKey);

/// <summary>Gets the key that defines the resource for the <see cref="ControlTemplate"/> of a Visual Studio-themed <see cref="RichTextBox"/> style.</summary>
public static object RichTextBoxControlTemplateKey { get; } = _prefix + nameof(RichTextBoxControlTemplateKey);

private static Uri BuildPackUri(string resource)
{
// Multiple versions of the toolkti assembly might be loaded, so when
// Multiple versions of the toolkit assembly might be loaded, so when
// loading a resource, we need to include the version number of this
// assembly to ensure that the resource is loaded from the correct assembly.
AssemblyName assemblyName = typeof(ToolkitResourceKeys).Assembly.GetName();
Expand Down

0 comments on commit 2020952

Please sign in to comment.