Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a Trigger to MouseOver Property of ValidationMessage #3540

Merged
merged 4 commits into from Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/MahApps.Metro.Samples/MahApps.Metro.Demo/App.xaml
Expand Up @@ -19,6 +19,7 @@

<Style BasedOn="{StaticResource MetroValidationPopup}" TargetType="{x:Type controls:CustomValidationPopup}">
<Setter Property="CloseOnMouseLeftButtonDown" Value="False" />
<Setter Property="ShowValidationErrorOnMouseOver" Value="False" />
</Style>

<!-- quick change of the collapse storyboard duration -->
Expand Down
Expand Up @@ -239,8 +239,9 @@
OnLabel="Enabled"
Style="{StaticResource MahApps.Metro.Styles.ToggleSwitch.Win10}" />
<Controls:ToggleSwitch x:Name="visibleSwitch"
Controls:ValidationHelper.ShowValidationErrorOnMouseOver="True"
Margin="{StaticResource ControlMargin}"
IsChecked="False"
IsChecked="{Binding IsToggleSwitchVisible, ValidatesOnDataErrors=True}"
OffLabel="Collapsed"
OnLabel="Visible"
Style="{StaticResource MahApps.Metro.Styles.ToggleSwitch.Win10}" />
Expand Down
Expand Up @@ -300,6 +300,11 @@ public bool IsHamburgerMenuPaneOpen
return "No time given!";
}

if (columnName == nameof(IsToggleSwitchVisible) && !IsToggleSwitchVisible)
{
return "There is something hidden... \nActivate me to show it up.";
}

return null;
}
}
Expand Down Expand Up @@ -455,5 +460,7 @@ private void ToggleIconScaling(object obj)
public bool IsScaleDownLargerFrame => ((MetroWindow)Application.Current.MainWindow).IconScalingMode == MultiFrameImageMode.ScaleDownLargerFrame;

public bool IsNoScaleSmallerFrame => ((MetroWindow)Application.Current.MainWindow).IconScalingMode == MultiFrameImageMode.NoScaleSmallerFrame;

public bool IsToggleSwitchVisible { get; set; }
}
}
33 changes: 26 additions & 7 deletions src/MahApps.Metro/Controls/CustomValidationPopup.cs
Expand Up @@ -18,15 +18,13 @@ namespace MahApps.Metro.Controls
/// </summary>
public class CustomValidationPopup : Popup
{
public static readonly DependencyProperty CloseOnMouseLeftButtonDownProperty = DependencyProperty.Register("CloseOnMouseLeftButtonDown", typeof(bool), typeof(CustomValidationPopup), new PropertyMetadata(true));

private Window hostWindow;

public CustomValidationPopup()
{
this.Loaded += this.CustomValidationPopup_Loaded;
this.Opened += this.CustomValidationPopup_Opened;
}
public static readonly DependencyProperty CloseOnMouseLeftButtonDownProperty
= DependencyProperty.Register(nameof(CloseOnMouseLeftButtonDown),
typeof(bool),
typeof(CustomValidationPopup),
new PropertyMetadata(true));

/// <summary>
/// Gets/sets if the popup can be closed by left mouse button down.
Expand All @@ -37,6 +35,24 @@ public bool CloseOnMouseLeftButtonDown
set { SetValue(CloseOnMouseLeftButtonDownProperty, value); }
}

public static readonly DependencyProperty ShowValidationErrorOnMouseOverProperty
= DependencyProperty.RegisterAttached(nameof(ShowValidationErrorOnMouseOver),
typeof(bool),
typeof(CustomValidationPopup),
new PropertyMetadata(false));

public bool ShowValidationErrorOnMouseOver
{
get { return (bool)GetValue(ShowValidationErrorOnMouseOverProperty); }
set { SetValue(ShowValidationErrorOnMouseOverProperty, value); }
}

public CustomValidationPopup()
{
this.Loaded += this.CustomValidationPopup_Loaded;
this.Opened += this.CustomValidationPopup_Opened;
}

protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
{
if (CloseOnMouseLeftButtonDown)
Expand Down Expand Up @@ -98,6 +114,7 @@ private void CustomValidationPopup_Unloaded(object sender, RoutedEventArgs e)
{
target.SizeChanged -= this.hostWindow_SizeOrLocationChanged;
}

if (this.hostWindow != null)
{
this.hostWindow.LocationChanged -= this.hostWindow_SizeOrLocationChanged;
Expand All @@ -106,6 +123,7 @@ private void CustomValidationPopup_Unloaded(object sender, RoutedEventArgs e)
this.hostWindow.Activated -= this.hostWindow_Activated;
this.hostWindow.Deactivated -= this.hostWindow_Deactivated;
}

this.Unloaded -= this.CustomValidationPopup_Unloaded;
this.Opened -= this.CustomValidationPopup_Opened;
this.hostWindow = null;
Expand Down Expand Up @@ -157,6 +175,7 @@ private void SetTopmostState(bool isTop)
{
return;
}

var hwnd = hwndSource.Handle;

#pragma warning disable 618
Expand Down
28 changes: 28 additions & 0 deletions src/MahApps.Metro/Controls/Helper/ValidationHelper.cs
@@ -0,0 +1,28 @@
using System.ComponentModel;
using System.Windows;

namespace MahApps.Metro.Controls
{
public static class ValidationHelper
{
public static readonly DependencyProperty ShowValidationErrorOnMouseOverProperty
= DependencyProperty.RegisterAttached("ShowValidationErrorOnMouseOver",
typeof(bool),
typeof(ValidationHelper),
new PropertyMetadata(false));

[Category(AppName.MahApps)]
[AttachedPropertyBrowsableForType(typeof(UIElement))]
public static bool GetShowValidationErrorOnMouseOver(UIElement element)
{
return (bool)element.GetValue(ShowValidationErrorOnMouseOverProperty);
}

[Category(AppName.MahApps)]
[AttachedPropertyBrowsableForType(typeof(UIElement))]
public static void SetShowValidationErrorOnMouseOver(UIElement element, bool value)
{
element.SetValue(ShowValidationErrorOnMouseOverProperty, value);
}
}
}
33 changes: 25 additions & 8 deletions src/MahApps.Metro/Styles/ValidationErrorTemplate.xaml
@@ -1,8 +1,8 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls">
xmlns:mah="clr-namespace:MahApps.Metro.Controls">

<Style x:Key="MetroValidationPopup" TargetType="{x:Type Controls:CustomValidationPopup}">
<Style x:Key="MetroValidationPopup" TargetType="{x:Type mah:CustomValidationPopup}">
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="HorizontalOffset" Value="0" />
<Setter Property="Placement" Value="Right" />
Expand All @@ -22,7 +22,8 @@
BorderThickness="1"
DataContext="{Binding ElementName=placeholder}">

<Grid Width="12"
<Grid x:Name="RedTriangle"
Width="12"
Height="12"
Margin="1 -4 -4 0"
HorizontalAlignment="Right"
Expand All @@ -37,10 +38,10 @@
</Grid>
</Border>

<Controls:CustomValidationPopup x:Name="ValidationPopup"
AllowsTransparency="True"
IsOpen="False"
PlacementTarget="{Binding ElementName=PopupTargetElement, Mode=OneWay}">
<mah:CustomValidationPopup x:Name="ValidationPopup"
AllowsTransparency="True"
IsOpen="False"
PlacementTarget="{Binding ElementName=PopupTargetElement, Mode=OneWay}">
<Grid x:Name="Root" Margin="4">
<Grid.Resources>
<Style TargetType="Border">
Expand Down Expand Up @@ -113,7 +114,7 @@
<ItemsControl ItemsSource="{Binding}" />
</Border>
</Grid>
</Controls:CustomValidationPopup>
</mah:CustomValidationPopup>
</Grid>
</AdornedElementPlaceholder>

Expand All @@ -125,6 +126,22 @@
</MultiDataTrigger.Conditions>
<Setter TargetName="ValidationPopup" Property="IsOpen" Value="True" />
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=RedTriangle, Path=IsMouseOver, Mode=OneWay}" Value="True" />
<Condition Binding="{Binding ElementName=placeholder, Path=AdornedElement.(Validation.HasError), Mode=OneWay}" Value="True" />
<Condition Binding="{Binding ElementName=placeholder, Path=AdornedElement.(mah:ValidationHelper.ShowValidationErrorOnMouseOver), Mode=OneWay}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter TargetName="ValidationPopup" Property="IsOpen" Value="True" />
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=RedTriangle, Path=IsMouseOver, Mode=OneWay}" Value="True" />
<Condition Binding="{Binding ElementName=placeholder, Path=AdornedElement.(Validation.HasError), Mode=OneWay}" Value="True" />
<Condition Binding="{Binding ElementName=ValidationPopup, Path=ShowValidationErrorOnMouseOver, Mode=OneWay}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter TargetName="ValidationPopup" Property="IsOpen" Value="True" />
</MultiDataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>

Expand Down