refactor: 将Framework库和RT、UI放在同一个解决方案中。
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Sicentury.Core.AttachedProperties
|
||||
{
|
||||
public static class TextBlockHighlighter
|
||||
{
|
||||
public static string GetSelection(DependencyObject obj)
|
||||
{
|
||||
return (string)obj.GetValue(SelectionProperty);
|
||||
}
|
||||
|
||||
public static void SetSelection(DependencyObject obj, string value)
|
||||
{
|
||||
obj.SetValue(SelectionProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty SelectionProperty =
|
||||
DependencyProperty.RegisterAttached("Selection", typeof(string), typeof(TextBlockHighlighter),
|
||||
new PropertyMetadata(new PropertyChangedCallback(SelectText)));
|
||||
|
||||
private static void SelectText(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if (d == null)
|
||||
return;
|
||||
if (!(d is TextBlock txtBlock))
|
||||
throw new InvalidOperationException("Only valid for TextBlock");
|
||||
|
||||
var text = txtBlock.Text;
|
||||
if (string.IsNullOrEmpty(text))
|
||||
return;
|
||||
|
||||
var highlightText = (string)txtBlock.GetValue(SelectionProperty);
|
||||
if (string.IsNullOrEmpty(highlightText))
|
||||
{
|
||||
txtBlock.Inlines.Clear();
|
||||
txtBlock.Inlines.Add(text);
|
||||
return;
|
||||
}
|
||||
|
||||
var index = text.IndexOf(highlightText, StringComparison.CurrentCultureIgnoreCase);
|
||||
if (index < 0)
|
||||
return;
|
||||
|
||||
var selectionColor = (Brush)txtBlock.GetValue(HighlightColorProperty);
|
||||
var foreColor = (Brush)txtBlock.GetValue(ForeColorProperty);
|
||||
|
||||
txtBlock.Inlines.Clear();
|
||||
while (true)
|
||||
{
|
||||
txtBlock.Inlines.AddRange(new Inline[]
|
||||
{
|
||||
new Run(text.Substring(0, index)),
|
||||
new Run(text.Substring(index, highlightText.Length))
|
||||
{
|
||||
Background = selectionColor,
|
||||
Foreground = foreColor
|
||||
}
|
||||
});
|
||||
|
||||
text = text.Substring(index + highlightText.Length);
|
||||
index = text.IndexOf(highlightText, StringComparison.CurrentCultureIgnoreCase);
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
txtBlock.Inlines.Add(new Run(text));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Brush GetHighlightColor(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(HighlightColorProperty);
|
||||
}
|
||||
|
||||
public static void SetHighlightColor(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(HighlightColorProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty HighlightColorProperty =
|
||||
DependencyProperty.RegisterAttached("HighlightColor", typeof(Brush), typeof(TextBlockHighlighter),
|
||||
new PropertyMetadata(Brushes.Yellow, new PropertyChangedCallback(SelectText)));
|
||||
|
||||
|
||||
public static Brush GetForeColor(DependencyObject obj)
|
||||
{
|
||||
return (Brush)obj.GetValue(ForeColorProperty);
|
||||
}
|
||||
|
||||
public static void SetForeColor(DependencyObject obj, Brush value)
|
||||
{
|
||||
obj.SetValue(ForeColorProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty ForeColorProperty =
|
||||
DependencyProperty.RegisterAttached("ForeColor", typeof(Brush), typeof(TextBlockHighlighter),
|
||||
new PropertyMetadata(Brushes.Black, new PropertyChangedCallback(SelectText)));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Sicentury.Core.AttachedProperties
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Adorner for the watermark
|
||||
/// </summary>
|
||||
internal class WatermarkAdorner : Adorner
|
||||
{
|
||||
#region Private Fields
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="ContentPresenter"/> that holds the watermark
|
||||
/// </summary>
|
||||
private readonly ContentPresenter _contentPresenter;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WatermarkAdorner"/> class
|
||||
/// </summary>
|
||||
/// <param name="adornedElement"><see cref="UIElement"/> to be adorned</param>
|
||||
/// <param name="watermark">The watermark</param>
|
||||
public WatermarkAdorner(UIElement adornedElement, object watermark) :
|
||||
base(adornedElement)
|
||||
{
|
||||
IsHitTestVisible = false;
|
||||
|
||||
_contentPresenter = new ContentPresenter
|
||||
{
|
||||
Content = watermark,
|
||||
Opacity = 0.3,
|
||||
Margin = new Thickness(Control.Margin.Left + Control.Padding.Left,
|
||||
Control.Margin.Top + Control.Padding.Top, 0, 0)
|
||||
};
|
||||
|
||||
if (Control is ItemsControl && !(Control is ComboBox))
|
||||
{
|
||||
_contentPresenter.VerticalAlignment = VerticalAlignment.Center;
|
||||
_contentPresenter.HorizontalAlignment = HorizontalAlignment.Center;
|
||||
}
|
||||
|
||||
// Hide the control adorner when the adorned element is hidden
|
||||
var binding = new Binding("IsVisible")
|
||||
{
|
||||
Source = adornedElement,
|
||||
Converter = new BooleanToVisibilityConverter()
|
||||
};
|
||||
SetBinding(VisibilityProperty, binding);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Protected Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of children for the <see cref="ContainerVisual"/>.
|
||||
/// </summary>
|
||||
protected override int VisualChildrenCount => 1;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets the control that is being adorned
|
||||
/// </summary>
|
||||
private Control Control => (Control)AdornedElement;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Protected Overrides
|
||||
|
||||
/// <summary>
|
||||
/// Returns a specified child <see cref="Visual"/> for the parent <see cref="ContainerVisual"/>.
|
||||
/// </summary>
|
||||
/// <param name="index">A 32-bit signed integer that represents the index value of the child <see cref="Visual"/>. The value of index must be between 0 and <see cref="VisualChildrenCount"/> - 1.</param>
|
||||
/// <returns>The child <see cref="Visual"/>.</returns>
|
||||
protected override Visual GetVisualChild(int index)
|
||||
{
|
||||
return _contentPresenter;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implements any custom measuring behavior for the adorner.
|
||||
/// </summary>
|
||||
/// <param name="constraint">A size to constrain the adorner to.</param>
|
||||
/// <returns>A <see cref="Size"/> object representing the amount of layout space needed by the adorner.</returns>
|
||||
protected override Size MeasureOverride(Size constraint)
|
||||
{
|
||||
// Here's the secret to getting the adorner to cover the whole control
|
||||
_contentPresenter.Measure(Control.RenderSize);
|
||||
return Control.RenderSize;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When overridden in a derived class, positions child elements and determines a size for a <see cref="FrameworkElement"/> derived class.
|
||||
/// </summary>
|
||||
/// <param name="finalSize">The final area within the parent that this element should use to arrange itself and its children.</param>
|
||||
/// <returns>The actual size used.</returns>
|
||||
protected override Size ArrangeOverride(Size finalSize)
|
||||
{
|
||||
_contentPresenter.Arrange(new Rect(finalSize));
|
||||
return finalSize;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Documents;
|
||||
|
||||
namespace Sicentury.Core.AttachedProperties
|
||||
{
|
||||
/// <summary>
|
||||
/// Class that provides the Watermark attached property
|
||||
/// </summary>
|
||||
public static class WatermarkService
|
||||
{
|
||||
/// <summary>
|
||||
/// Watermark Attached Dependency Property
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty WatermarkProperty = DependencyProperty.RegisterAttached(
|
||||
"Watermark",
|
||||
typeof(object),
|
||||
typeof(WatermarkService),
|
||||
new FrameworkPropertyMetadata(null, OnWatermarkChanged));
|
||||
|
||||
#region Private Fields
|
||||
|
||||
/// <summary>
|
||||
/// Dictionary of ItemsControls
|
||||
/// </summary>
|
||||
private static readonly Dictionary<object, ItemsControl> ItemsControls
|
||||
= new Dictionary<object, ItemsControl>();
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Watermark property. This dependency property indicates the watermark for the control.
|
||||
/// </summary>
|
||||
/// <param name="d"><see cref="DependencyObject"/> to get the property from</param>
|
||||
/// <returns>The value of the Watermark property</returns>
|
||||
public static object GetWatermark(DependencyObject d)
|
||||
{
|
||||
return d.GetValue(WatermarkProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the Watermark property. This dependency property indicates the watermark for the control.
|
||||
/// </summary>
|
||||
/// <param name="d"><see cref="DependencyObject"/> to set the property on</param>
|
||||
/// <param name="value">value of the property</param>
|
||||
public static void SetWatermark(DependencyObject d, object value)
|
||||
{
|
||||
d.SetValue(WatermarkProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles changes to the Watermark property.
|
||||
/// </summary>
|
||||
/// <param name="d"><see cref="DependencyObject"/> that fired the event</param>
|
||||
/// <param name="e">A <see cref="DependencyPropertyChangedEventArgs"/> that contains the event data.</param>
|
||||
private static void OnWatermarkChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var control = (Control)d;
|
||||
control.Loaded += Control_Loaded;
|
||||
|
||||
if (d is ComboBox)
|
||||
{
|
||||
control.GotKeyboardFocus += Control_GotKeyboardFocus;
|
||||
control.LostKeyboardFocus += Control_Loaded;
|
||||
}
|
||||
else if (d is TextBox)
|
||||
{
|
||||
control.GotKeyboardFocus += Control_GotKeyboardFocus;
|
||||
control.LostKeyboardFocus += Control_Loaded;
|
||||
((TextBox)control).TextChanged += Control_GotKeyboardFocus;
|
||||
}
|
||||
|
||||
if (d is ItemsControl o && !(o is ComboBox))
|
||||
{
|
||||
// for Items property
|
||||
o.ItemContainerGenerator.ItemsChanged += ItemsChanged;
|
||||
ItemsControls.Add(o.ItemContainerGenerator, o);
|
||||
|
||||
// for ItemsSource property
|
||||
var prop = DependencyPropertyDescriptor.FromProperty(ItemsControl.ItemsSourceProperty, o.GetType());
|
||||
prop.AddValueChanged(o, ItemsSourceChanged);
|
||||
}
|
||||
}
|
||||
|
||||
#region Event Handlers
|
||||
|
||||
/// <summary>
|
||||
/// Handle the GotFocus event on the control
|
||||
/// </summary>
|
||||
/// <param name="sender">The source of the event.</param>
|
||||
/// <param name="e">A <see cref="RoutedEventArgs"/> that contains the event data.</param>
|
||||
private static void Control_GotKeyboardFocus(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var c = (Control)sender;
|
||||
if (ShouldShowWatermark(c))
|
||||
{
|
||||
ShowWatermark(c);
|
||||
}
|
||||
else
|
||||
{
|
||||
RemoveWatermark(c);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle the Loaded and LostFocus event on the control
|
||||
/// </summary>
|
||||
/// <param name="sender">The source of the event.</param>
|
||||
/// <param name="e">A <see cref="RoutedEventArgs"/> that contains the event data.</param>
|
||||
private static void Control_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var control = (Control)sender;
|
||||
if (ShouldShowWatermark(control))
|
||||
{
|
||||
ShowWatermark(control);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event handler for the items source changed event
|
||||
/// </summary>
|
||||
/// <param name="sender">The source of the event.</param>
|
||||
/// <param name="e">A <see cref="EventArgs"/> that contains the event data.</param>
|
||||
private static void ItemsSourceChanged(object sender, System.EventArgs e)
|
||||
{
|
||||
var c = (ItemsControl)sender;
|
||||
if (c.ItemsSource != null)
|
||||
{
|
||||
if (ShouldShowWatermark(c))
|
||||
{
|
||||
ShowWatermark(c);
|
||||
}
|
||||
else
|
||||
{
|
||||
RemoveWatermark(c);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ShowWatermark(c);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event handler for the items changed event
|
||||
/// </summary>
|
||||
/// <param name="sender">The source of the event.</param>
|
||||
/// <param name="e">A <see cref="ItemsChangedEventArgs"/> that contains the event data.</param>
|
||||
private static void ItemsChanged(object sender, ItemsChangedEventArgs e)
|
||||
{
|
||||
ItemsControl control;
|
||||
if (ItemsControls.TryGetValue(sender, out control))
|
||||
{
|
||||
if (ShouldShowWatermark(control))
|
||||
{
|
||||
ShowWatermark(control);
|
||||
}
|
||||
else
|
||||
{
|
||||
RemoveWatermark(control);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helper Methods
|
||||
|
||||
/// <summary>
|
||||
/// Remove the watermark from the specified element
|
||||
/// </summary>
|
||||
/// <param name="control">Element to remove the watermark from</param>
|
||||
private static void RemoveWatermark(UIElement control)
|
||||
{
|
||||
var layer = AdornerLayer.GetAdornerLayer(control);
|
||||
|
||||
// layer could be null if control is no longer in the visual tree
|
||||
if (layer != null)
|
||||
{
|
||||
var adorners = layer.GetAdorners(control);
|
||||
if (adorners == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var adorner in adorners)
|
||||
{
|
||||
if (adorner is WatermarkAdorner)
|
||||
{
|
||||
adorner.Visibility = Visibility.Hidden;
|
||||
layer.Remove(adorner);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Show the watermark on the specified control
|
||||
/// </summary>
|
||||
/// <param name="control">Control to show the watermark on</param>
|
||||
private static void ShowWatermark(Control control)
|
||||
{
|
||||
var layer = AdornerLayer.GetAdornerLayer(control);
|
||||
|
||||
// layer could be null if control is no longer in the visual tree
|
||||
layer?.Add(new WatermarkAdorner(control, GetWatermark(control)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether or not the watermark should be shown on the specified control
|
||||
/// </summary>
|
||||
/// <param name="c"><see cref="Control"/> to test</param>
|
||||
/// <returns>true if the watermark should be shown; false otherwise</returns>
|
||||
private static bool ShouldShowWatermark(Control c)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case ComboBox box:
|
||||
return box.Text == string.Empty;
|
||||
|
||||
case TextBoxBase _:
|
||||
return (c as TextBox)?.Text == string.Empty;
|
||||
|
||||
case ItemsControl control:
|
||||
return control.Items.Count == 0;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user