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

Feature/auto watermark #2722

Merged
merged 24 commits into from
Nov 19, 2016
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1348758
[Introducing] AutoWatermark #2712
michaelmairegger Oct 29, 2016
705d8b1
Add property mapping for controls
michaelmairegger Oct 29, 2016
148116a
Add AttachedPropertyBrowsableForType-Attribute
michaelmairegger Oct 29, 2016
c46a7e4
Add WatermarkAttribute
michaelmairegger Oct 29, 2016
d4222be
Add NET4_5 compiler switch
michaelmairegger Oct 29, 2016
165286f
Add example to DateExamples.xaml
michaelmairegger Oct 29, 2016
28a2f1f
Merge branch 'develop' into feature/AutoWatermark
punker76 Oct 29, 2016
df52d98
Add code to support auto-watermark on <NET4.5 too
michaelmairegger Oct 31, 2016
d765597
Use DisplayAttribute instead of WatermarkAttribute
michaelmairegger Oct 31, 2016
df4eb09
Use GetDescription instead of Description to support localizable strings
michaelmairegger Oct 31, 2016
eb3f0e1
Revert XAML-Style changes introduced by mistake
michaelmairegger Oct 31, 2016
c670038
Code cleanup
michaelmairegger Oct 31, 2016
56d7da8
Add Dependency Property documentation
michaelmairegger Oct 31, 2016
3287af5
Subscribe to event only in case of Autowatermark is true.
michaelmairegger Oct 31, 2016
1b1bd81
[Fix] Issue with DOT-binding
michaelmairegger Oct 31, 2016
516d252
[Fix] Issue when using Array Indexer as binding path
michaelmairegger Oct 31, 2016
3319d95
[Fix] Issue whith using . Paths in NET4
michaelmairegger Nov 1, 2016
3019f75
Allow binding to element of collection
michaelmairegger Nov 1, 2016
f281341
Allow Write-Only Properties too.
michaelmairegger Nov 1, 2016
ac0eb8e
Extracted Methods for better understanding
michaelmairegger Nov 2, 2016
681752e
Adding XML-Documentation
michaelmairegger Nov 2, 2016
1c02520
Use Display.Prompt intead of Display.Description
michaelmairegger Nov 16, 2016
210841a
Merge branch 'develop' into feature/AutoWatermark
punker76 Nov 18, 2016
eb7176f
Merge branch 'develop' into feature/AutoWatermark
punker76 Nov 18, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace MetroDemo.ExampleViews
{
using System.Windows;
using MahApps.Metro.Controls;

/// <summary>
/// Interaction logic for DateExamples.xaml
/// </summary>
Expand All @@ -10,6 +13,10 @@ public partial class DateExamples : UserControl
public DateExamples()
{
InitializeComponent();
#if NET4_5
this.AutoWatermark.Visibility = Visibility.Visible;
this.AutoWatermark.SetValue(TextBoxHelper.AutoWatermarkProperty, true);
#endif
}
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
using MetroDemo.ExampleViews;
using NHotkey;
using NHotkey.Wpf;
#if NET4_5
using MahApps.Metro.Controls.Helper;
#endif

namespace MetroDemo
{
Expand Down Expand Up @@ -125,6 +128,10 @@ public int? IntegerGreater10Property
}

DateTime? _datePickerDate;

#if NET4_5
[Watermark("Auto resolved Watermark")]
#endif
public DateTime? DatePickerDate
{
get { return this._datePickerDate; }
Expand Down Expand Up @@ -254,6 +261,7 @@ public string this[string columnName]
}
}

[Description("Test-Property")]
public string Error { get { return string.Empty; } }

private ICommand singleCloseTabCommand;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.ComponentModel;
#if NET4_5
using System.Collections.Generic;
using System.Reflection;
using MahApps.Metro.Controls.Helper;
#endif

namespace MahApps.Metro.Controls
{
using System.ComponentModel;

/// <summary>
/// A helper class that provides various attached properties for the TextBox control.
Expand Down Expand Up @@ -50,6 +55,20 @@ public class TextBoxHelper

public static readonly DependencyProperty IsSpellCheckContextMenuEnabledProperty = DependencyProperty.RegisterAttached("IsSpellCheckContextMenuEnabled", typeof(bool), typeof(TextBoxHelper), new FrameworkPropertyMetadata(false, UseSpellCheckContextMenuChanged));

#if NET4_5
public static readonly DependencyProperty AutoWatermarkProperty = DependencyProperty.RegisterAttached("AutoWatermark", typeof(bool), typeof(TextBoxHelper), new PropertyMetadata(default(bool), OnAutoWatermarkChanged));

private static readonly Dictionary<Type, DependencyProperty> AutoWatermarkPropertyMapping = new Dictionary<Type, DependencyProperty>
{
{ typeof(TextBox), TextBox.TextProperty },
{ typeof(ComboBox), Selector.SelectedItemProperty },
{ typeof(NumericUpDown), NumericUpDown.ValueProperty },
{ typeof(DatePicker), DatePicker.SelectedDateProperty },
{ typeof(TimePicker), TimePickerBase.SelectedTimeProperty },
{ typeof(DateTimePicker), DateTimePicker.SelectedDateProperty }
};
#endif

/// <summary>
/// Indicates if a TextBox or RichTextBox should use SpellCheck context menu
/// </summary>
Expand All @@ -66,6 +85,61 @@ public static void SetIsSpellCheckContextMenuEnabled(UIElement element, bool val
element.SetValue(IsSpellCheckContextMenuEnabledProperty, value);
}

#if NET4_5
[Category(AppName.MahApps)]
[AttachedPropertyBrowsableForType(typeof(TextBox))]
[AttachedPropertyBrowsableForType(typeof(ComboBox))]
[AttachedPropertyBrowsableForType(typeof(DatePicker))]
[AttachedPropertyBrowsableForType(typeof(TimePickerBase))]
[AttachedPropertyBrowsableForType(typeof(NumericUpDown))]
public static bool GetAutoWatermark(DependencyObject element)
{
return (bool)element.GetValue(AutoWatermarkProperty);
}

public static void SetAutoWatermark(DependencyObject element, bool value)
{
element.SetValue(AutoWatermarkProperty, value);
}

private static void OnAutoWatermarkChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = d as FrameworkElement;
if (element != null)
{
element.Loaded += OnControlWithAutoWatermarkSupportLoaded;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible memory leak...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the event subscription? Shouldn't it be enough to unsubscribe directly after the event was raised the first time?

}
}

private static void OnControlWithAutoWatermarkSupportLoaded(object o, RoutedEventArgs routedEventArgs)
{
FrameworkElement obj = (FrameworkElement)o;
obj.Loaded -= OnControlWithAutoWatermarkSupportLoaded;

DependencyProperty dependencyProperty;

if (!AutoWatermarkPropertyMapping.TryGetValue(obj.GetType(), out dependencyProperty))
{
throw new NotSupportedException($"{nameof(AutoWatermarkProperty)} is not supported for {obj.GetType()}");
}
var binding = obj.GetBindingExpression(dependencyProperty);

if (binding != null)
{
var dataItem = binding.DataItem.GetType();
var property = dataItem.GetProperty(binding.ResolvedSourcePropertyName, BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance);
if (property != null)
{
var attribute = property.GetCustomAttribute<WatermarkAttribute>();
if (attribute != null)
{
obj.SetValue(WatermarkProperty, attribute.Caption);
}
}
}
}
#endif

private static void UseSpellCheckContextMenuChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var tb = d as TextBoxBase;
Expand Down Expand Up @@ -212,7 +286,7 @@ public static void SetUseFloatingWatermark(DependencyObject obj, bool value)
{
obj.SetValue(UseFloatingWatermarkProperty, value);
}

/// <summary>
/// Gets if the attached TextBox has text.
/// </summary>
Expand Down Expand Up @@ -310,15 +384,15 @@ private static void OnIsMonitoringChanged(DependencyObject d, DependencyProperty
}
}
}

private static void SetTextLength<TDependencyObject>(TDependencyObject sender, Func<TDependencyObject, int> funcTextLength) where TDependencyObject : DependencyObject
{
if (sender != null)
{
var value = funcTextLength(sender);
sender.SetValue(TextLengthProperty, value);
sender.SetValue(HasTextProperty, value >= 1);
}
}
}

private static void TextChanged(object sender, RoutedEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace MahApps.Metro.Controls.Helper
{
#if NET4_5
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class WatermarkAttribute : Attribute
{
public WatermarkAttribute(string caption)
{
this.Caption = caption;
}

public string Caption { get; set; }
}
#endif
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it necessary to introduce a new attribute? Is the Display attribute too bad?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, of course, it is not.

Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Controls\Helper\TextBoxHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Helper\ToggleButtonHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Helper\VisibilityHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Helper\WatermarkAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\HotKeyBox.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\IMetroThumb.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\LayoutInvalidationCatcher.cs" />
Expand Down