Skip to content

Commit

Permalink
Add "pressed" state to Hyperlink
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara committed Mar 31, 2020
1 parent 1270e86 commit 19da839
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 21 deletions.
20 changes: 16 additions & 4 deletions ModernWpf.SampleApp/ControlPages/HyperlinkButtonPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
xmlns:ui="http://schemas.modernwpf.com/2019"
xmlns:sc="clr-namespace:SamplesCommon;assembly=SamplesCommon"
mc:Ignorable="d"
d:DesignHeight="450"
d:DesignWidth="800"
Style="{StaticResource PageStyle}"
Title="HyperlinkButtonPage">
Style="{StaticResource PageStyle}">

<ScrollViewer>
<ui:SimpleStackPanel Style="{StaticResource ControlPageContentPanelStyle}">
Expand All @@ -37,6 +34,21 @@
Content="Go to ListBox"
Click="GoToHyperlinkButton_Click" />
</sc:ControlExample>

<sc:ControlExample
HeaderText="Hyperlink text."
VerticalContentAlignment="Center">
<TextBlock x:Name="Control3">
<Run>This is</Run>
<Hyperlink>hyperlink text</Hyperlink>
</TextBlock>

<sc:ControlExample.Options>
<CheckBox
Content="IsEnabled"
IsChecked="{Binding ElementName=Control3, Path=IsEnabled}" />
</sc:ControlExample.Options>
</sc:ControlExample>
</ui:SimpleStackPanel>
</ScrollViewer>
</Page>
82 changes: 82 additions & 0 deletions ModernWpf/Controls/Primitives/HyperlinkHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;

namespace ModernWpf.Controls.Primitives
{
public static class HyperlinkHelper
{
#region IsPressEnabled

private static readonly DependencyProperty IsPressEnabledProperty =
DependencyProperty.RegisterAttached(
"IsPressEnabled",
typeof(bool),
typeof(HyperlinkHelper),
new PropertyMetadata(false, OnIsPressEnabledChanged));

public static bool GetIsPressEnabled(Hyperlink hyperlink)
{
return (bool)hyperlink.GetValue(IsPressEnabledProperty);
}

public static void SetIsPressEnabled(Hyperlink hyperlink, bool value)
{
hyperlink.SetValue(IsPressEnabledProperty, value);
}

private static void OnIsPressEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var hyperlink = (Hyperlink)d;

if ((bool)e.NewValue)
{
hyperlink.AddHandler(Hyperlink.MouseLeftButtonDownEvent, new MouseButtonEventHandler(OnMouseLeftButtonDown), true);
hyperlink.AddHandler(Hyperlink.MouseLeftButtonUpEvent, new MouseButtonEventHandler(OnMouseLeftButtonUp), true);
}
else
{
hyperlink.RemoveHandler(Hyperlink.MouseLeftButtonDownEvent, new MouseButtonEventHandler(OnMouseLeftButtonDown));
hyperlink.RemoveHandler(Hyperlink.MouseLeftButtonUpEvent, new MouseButtonEventHandler(OnMouseLeftButtonUp));
}
}

private static void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var hyperlink = (Hyperlink)sender;
if (hyperlink.IsMouseCaptured && e.ButtonState == MouseButtonState.Pressed)
{
hyperlink.SetValue(IsPressedPropertyKey, true);
}
}

private static void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var hyperlink = (Hyperlink)sender;
if (GetIsPressed(hyperlink))
{
hyperlink.SetValue(IsPressedPropertyKey, false);
}
}

#endregion

#region IsPressed

private static readonly DependencyPropertyKey IsPressedPropertyKey =
DependencyProperty.RegisterAttachedReadOnly(
"IsPressed",
typeof(bool),
typeof(HyperlinkHelper),
new PropertyMetadata(false));

public static readonly DependencyProperty IsPressedProperty = IsPressedPropertyKey.DependencyProperty;

public static bool GetIsPressed(Hyperlink hyperlink)
{
return (bool)hyperlink.GetValue(IsPressedProperty);
}

#endregion
}
}
1 change: 1 addition & 0 deletions ModernWpf/ControlsResources.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<ResourceDictionary Source="Styles/GridSplitter.xaml" />
<ResourceDictionary Source="Styles/GroupBox.xaml" />
<ResourceDictionary Source="Styles/GroupItem.xaml" />
<ResourceDictionary Source="Styles/Hyperlink.xaml" />
<ResourceDictionary Source="Styles/Label.xaml" />
<ResourceDictionary Source="Styles/ListBox.xaml" />
<ResourceDictionary Source="Styles/ListView.xaml" />
Expand Down
6 changes: 5 additions & 1 deletion ModernWpf/Styles/DataGrid.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -705,9 +705,13 @@
<Style TargetType="Hyperlink">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="TextDecorations" Value="Underline" />
<Setter Property="primitives:HyperlinkHelper.IsPressEnabled" Value="True" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="{DynamicResource SystemControlPageTextBaseMediumBrush}" />
<Setter Property="Foreground" Value="{DynamicResource SystemControlHyperlinkBaseMediumBrush}" />
</Trigger>
<Trigger Property="primitives:HyperlinkHelper.IsPressed" Value="true">
<Setter Property="Foreground" Value="{DynamicResource SystemControlHighlightBaseMediumLowBrush}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource SystemControlDisabledBaseMediumLowBrush}" />
Expand Down
27 changes: 27 additions & 0 deletions ModernWpf/Styles/Hyperlink.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:primitives="clr-namespace:ModernWpf.Controls.Primitives">

<Style x:Key="DefaultHyperlinkStyle" TargetType="Hyperlink">
<Setter Property="Foreground" Value="{DynamicResource SystemControlHyperlinkTextBrush}" />
<Setter Property="TextDecorations" Value="Underline" />
<Setter Property="primitives:HyperlinkHelper.IsPressEnabled" Value="True" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="{DynamicResource SystemControlHyperlinkBaseMediumBrush}" />
</Trigger>
<Trigger Property="primitives:HyperlinkHelper.IsPressed" Value="true">
<Setter Property="Foreground" Value="{DynamicResource SystemControlHighlightBaseMediumLowBrush}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource SystemControlDisabledBaseMediumLowBrush}" />
</Trigger>
<Trigger Property="IsEnabled" Value="true">
<Setter Property="Cursor" Value="Hand" />
</Trigger>
</Style.Triggers>
</Style>

<Style TargetType="Hyperlink" BasedOn="{StaticResource DefaultHyperlinkStyle}" />
</ResourceDictionary>
16 changes: 0 additions & 16 deletions ModernWpf/Styles/RichTextBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,6 @@

<Thickness x:Key="RichEditBoxTopHeaderMargin">0,0,0,4</Thickness>

<Style x:Key="{x:Type Hyperlink}" TargetType="{x:Type Hyperlink}">
<Setter Property="Foreground" Value="{DynamicResource SystemControlHyperlinkTextBrush}" />
<Setter Property="TextDecorations" Value="Underline" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="{DynamicResource SystemControlPageTextBaseMediumBrush}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource SystemControlDisabledBaseMediumLowBrush}" />
</Trigger>
<Trigger Property="IsEnabled" Value="true">
<Setter Property="Cursor" Value="Hand" />
</Trigger>
</Style.Triggers>
</Style>

<Style x:Key="DefaultRichTextBoxStyle" TargetType="RichTextBox">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Foreground" Value="{DynamicResource TextControlForeground}" />
Expand Down

0 comments on commit 19da839

Please sign in to comment.