support for .NET framework 3.5

Still have bugs
This commit is contained in:
NaBian committed 2019-01-21 00:43:10 +08:00
1 parent 5878039e21
commit 7d6b491f25
704 files changed
+48273 -33

No files matched your search

@@ -0,0 +1,232 @@
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using HandyControl.Data;
using HandyControl.Tools;
namespace HandyControl.Controls
{
public class Rate : RegularItemsControl
{
static Rate()
{
ItemWidthProperty.OverrideMetadata(typeof(Rate), new PropertyMetadata(ValueBoxes.Double20Box));
ItemHeightProperty.OverrideMetadata(typeof(Rate), new PropertyMetadata(ValueBoxes.Double20Box));
}
public static readonly DependencyProperty AllowHalfProperty = DependencyProperty.Register(
"AllowHalf", typeof(bool), typeof(Rate), new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty AllowClearProperty = DependencyProperty.Register(
"AllowClear", typeof(bool), typeof(Rate), new FrameworkPropertyMetadata(ValueBoxes.TrueBox, FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty IconProperty = DependencyProperty.Register(
"Icon", typeof(Geometry), typeof(Rate), new FrameworkPropertyMetadata(default(Geometry), FrameworkPropertyMetadataOptions.Inherits));
public static readonly DependencyProperty CountProperty = DependencyProperty.Register(
"Count", typeof(int), typeof(Rate), new PropertyMetadata(ValueBoxes.Int5Box));
public static readonly DependencyProperty DefaultValueProperty = DependencyProperty.Register(
"DefaultValue", typeof(double), typeof(Rate), new PropertyMetadata(ValueBoxes.Double0Box));
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
"Value", typeof(double), typeof(Rate), new PropertyMetadata(ValueBoxes.Double0Box));
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text", typeof(string), typeof(Rate), new PropertyMetadata(default(string)));
public static readonly DependencyProperty ShowTextProperty = DependencyProperty.Register(
"ShowText", typeof(bool), typeof(Rate), new PropertyMetadata(ValueBoxes.FalseBox));
public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register(
"IsReadOnly", typeof(bool), typeof(Rate), new PropertyMetadata(ValueBoxes.FalseBox));
private bool _isLoaded;
public Rate()
{
AddHandler(RateItem.SelectedChangedEvent, new RoutedEventHandler(RateItemSelectedChanged));
AddHandler(RateItem.ValueChangedEvent, new RoutedEventHandler(RateItemValueChanged));
Loaded += (s, e) =>
{
if (DesignerHelper.IsInDesignMode) return;
if (_isLoaded) return;
_isLoaded = true;
if (Value <= 0)
{
if (DefaultValue > 0)
{
Value = DefaultValue;
UpdateItems();
}
}
else
{
UpdateItems();
}
};
}
public bool AllowHalf
{
get => (bool) GetValue(AllowHalfProperty);
set => SetValue(AllowHalfProperty, value);
}
public bool AllowClear
{
get => (bool) GetValue(AllowClearProperty);
set => SetValue(AllowClearProperty, value);
}
public Geometry Icon
{
get => (Geometry) GetValue(IconProperty);
set => SetValue(IconProperty, value);
}
public int Count
{
get => (int) GetValue(CountProperty);
set => SetValue(CountProperty, value);
}
public double DefaultValue
{
get => (double) GetValue(DefaultValueProperty);
set => SetValue(DefaultValueProperty, value);
}
public double Value
{
get => (double) GetValue(ValueProperty);
set => SetValue(ValueProperty, value);
}
public string Text
{
get => (string) GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public bool ShowText
{
get => (bool) GetValue(ShowTextProperty);
set => SetValue(ShowTextProperty, value);
}
public bool IsReadOnly
{
get => (bool)GetValue(IsReadOnlyProperty);
set => SetValue(IsReadOnlyProperty, value);
}
private void RateItemValueChanged(object sender, RoutedEventArgs e)
{
Value =
(from RateItem item in Items where item.IsSelected select item.IsHalf ? 0.5 : 1).Sum();
}
private void RateItemSelectedChanged(object sender, RoutedEventArgs e)
{
if (e.OriginalSource is RateItem rateItem)
{
var index = rateItem.Index;
for (var i = 0; i < index; i++)
{
if (Items[i] is RateItem item)
{
item.IsSelected = true;
item.IsHalf = false;
}
}
for (var i = index; i < Count; i++)
{
if (Items[i] is RateItem item)
{
item.IsSelected = false;
item.IsHalf = false;
}
}
}
}
protected override bool IsItemItsOwnContainerOverride(object item)
{
return item is RateItem;
}
protected override DependencyObject GetContainerForItemOverride()
{
return new RateItem();
}
public override void OnApplyTemplate()
{
if (!_isLoaded)
{
Items.Clear();
for (var i = 1; i <= Count; i++)
{
Items.Add(new RateItem
{
Index = i
});
}
}
base.OnApplyTemplate();
}
protected override void OnMouseLeave(MouseEventArgs e)
{
base.OnMouseLeave(e);
UpdateItems();
}
protected override void UpdateItems()
{
if (!_isLoaded) return;
var count = (int) Value;
for (var i = 0; i < count; i++)
{
if (Items[i] is RateItem rateItem)
{
rateItem.IsSelected = true;
rateItem.IsHalf = false;
}
}
if (Value > count)
{
if (Items[count] is RateItem rateItem)
{
rateItem.IsSelected = true;
rateItem.IsHalf = true;
}
count += 1;
}
for (var i = count; i < Count; i++)
{
if (Items[i] is RateItem rateItem)
{
rateItem.IsSelected = false;
rateItem.IsHalf = false;
}
}
}
public void Reset()
{
Value = DefaultValue;
}
}
}
@@ -0,0 +1,215 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using HandyControl.Data;
using HandyControl.Tools.Extension;
namespace HandyControl.Controls
{
[TemplatePart(Name = ElementIcon, Type = typeof(FrameworkElement))]
public class RateItem : Control
{
private const string ElementIcon = "PART_Icon";
public static readonly DependencyProperty AllowClearProperty =
Rate.AllowClearProperty.AddOwner(typeof(RateItem));
public static readonly DependencyProperty AllowHalfProperty =
Rate.AllowHalfProperty.AddOwner(typeof(RateItem), new PropertyMetadata(OnAllowHalfChanged));
public static readonly DependencyProperty IconProperty = Rate.IconProperty.AddOwner(typeof(RateItem));
public static readonly DependencyProperty IsReadOnlyProperty = Rate.IsReadOnlyProperty.AddOwner(typeof(RateItem));
internal static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register(
"IsSelected", typeof(bool), typeof(RateItem),
new PropertyMetadata(ValueBoxes.FalseBox, OnIsSelectedChanged));
public static readonly RoutedEvent SelectedChangedEvent =
EventManager.RegisterRoutedEvent("SelectedChanged", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(RateItem));
public static readonly RoutedEvent ValueChangedEvent =
EventManager.RegisterRoutedEvent("ValueChanged", RoutingStrategy.Bubble,
typeof(RoutedEventHandler), typeof(RateItem));
private FrameworkElement _icon;
private bool _isHalf;
private bool _isLoaded;
private bool _isMouseLeftButtonDown;
private bool _isSentValue;
public RateItem()
{
Loaded += (s, e) => _isLoaded = true;
}
public bool AllowClear
{
get => (bool) GetValue(AllowClearProperty);
set => SetValue(AllowClearProperty, value);
}
public bool AllowHalf
{
get => (bool) GetValue(AllowHalfProperty);
set => SetValue(AllowHalfProperty, value);
}
public Geometry Icon
{
get => (Geometry) GetValue(IconProperty);
set => SetValue(IconProperty, value);
}
internal bool IsSelected
{
get => (bool) GetValue(IsSelectedProperty);
set => SetValue(IsSelectedProperty, value);
}
public bool IsReadOnly
{
get => (bool)GetValue(IsReadOnlyProperty);
set => SetValue(IsReadOnlyProperty, value);
}
internal bool IsHalf
{
get => _isHalf;
set
{
if (_isHalf == value) return;
_isHalf = value;
if (_icon == null) return;
_icon.Width = value ? ActualWidth / 2 : ActualWidth;
}
}
internal int Index { get; set; }
private static void OnAllowHalfChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctl = (RateItem) d;
ctl.HandleMouseMoveEvent((bool) e.NewValue);
}
private static void OnIsSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctl = (RateItem) d;
ctl._icon?.Show((bool) e.NewValue);
}
public event RoutedEventHandler SelectedChanged
{
add => AddHandler(SelectedChangedEvent, value);
remove => RemoveHandler(SelectedChangedEvent, value);
}
public event RoutedEventHandler ValueChanged
{
add => AddHandler(ValueChangedEvent, value);
remove => RemoveHandler(ValueChangedEvent, value);
}
private void HandleMouseMoveEvent(bool handle)
{
if (handle)
{
MouseMove += RateItem_MouseMove;
}
else
{
MouseMove -= RateItem_MouseMove;
}
}
private void RateItem_MouseMove(object sender, MouseEventArgs e)
{
if (IsReadOnly) return;
if (!AllowHalf) return;
var p = e.GetPosition(this);
IsHalf = p.X < ActualWidth / 2;
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_icon = GetTemplateChild(ElementIcon) as FrameworkElement;
if (_isLoaded)
{
if (_icon == null) return;
_icon.Show(IsSelected);
_icon.Width = IsHalf ? ActualWidth / 2 : ActualWidth;
}
}
protected override void OnMouseEnter(MouseEventArgs e)
{
base.OnMouseEnter(e);
if (IsReadOnly) return;
_isSentValue = false;
IsSelected = true;
RaiseEvent(new RoutedEventArgs(SelectedChangedEvent) {Source = this});
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
if (IsReadOnly) return;
_isMouseLeftButtonDown = true;
}
protected override void OnMouseLeave(MouseEventArgs e)
{
base.OnMouseLeave(e);
if (IsReadOnly) return;
_isMouseLeftButtonDown = false;
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonUp(e);
if (IsReadOnly) return;
if (_isMouseLeftButtonDown)
{
if (Index == 1 && AllowClear)
{
if (IsSelected)
{
if (!_isSentValue)
{
RaiseEvent(new RoutedEventArgs(ValueChangedEvent) {Source = this});
_isMouseLeftButtonDown = false;
_isSentValue = true;
return;
}
_isSentValue = false;
IsSelected = false;
IsHalf = false;
}
else
{
IsSelected = true;
if (AllowHalf)
{
var p = e.GetPosition(this);
IsHalf = p.X < ActualWidth / 2;
}
}
}
RaiseEvent(new RoutedEventArgs(ValueChangedEvent) {Source = this});
_isMouseLeftButtonDown = false;
}
}
}
}