-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
131 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters