清理无用代码
This commit is contained in:
@@ -1,44 +0,0 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace ShrlAlgoToolkit.Mvvm.Assists
|
||||
{
|
||||
public static class BindingHelper
|
||||
{
|
||||
public static Binding GetBinding(this FrameworkElement element, DependencyProperty dependencyProperty)
|
||||
{
|
||||
var expression = element.GetBindingExpression(dependencyProperty);
|
||||
if (expression == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return expression.ParentBinding;
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取绑定的源(模型视图)属性类型
|
||||
/// </summary>
|
||||
/// <param name="element"></param>
|
||||
/// <param name="dependencyProperty"></param>
|
||||
/// <returns></returns>
|
||||
public static Type GetBindingPropertyType(this FrameworkElement element, DependencyProperty dependencyProperty)
|
||||
{
|
||||
var binding = element.GetBindingExpression(dependencyProperty);
|
||||
var source = binding.ResolvedSource;
|
||||
var prop = source.GetType().GetProperty(binding.ResolvedSourcePropertyName);
|
||||
return prop.PropertyType;
|
||||
}
|
||||
/// <summary>
|
||||
/// 设置绑定
|
||||
/// </summary>
|
||||
/// <param name="element">wpf框架元素</param>
|
||||
/// <param name="dependencyProperty">依赖属性</param>
|
||||
/// <param name="propertyName">视图模型属性</param>
|
||||
/// <param name="sourceData">视图模型</param>
|
||||
public static void SetBinding(this FrameworkElement element,DependencyProperty dependencyProperty,string propertyName,object sourceData)
|
||||
{
|
||||
var description = new Binding() { Path = new PropertyPath(propertyName), Source = sourceData };
|
||||
element.SetBinding(dependencyProperty, description);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace ShrlAlgoToolkit.Mvvm.Assists
|
||||
{
|
||||
/// <summary>
|
||||
/// 绑定代理,用来传递绑定对象
|
||||
/// </summary>
|
||||
public class BindingProxy : Freezable
|
||||
{
|
||||
protected override Freezable CreateInstanceCore() => new BindingProxy();
|
||||
|
||||
public object Data
|
||||
{
|
||||
get { return GetValue(DataProperty); }
|
||||
set { SetValue(DataProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty DataProperty = DependencyProperty.Register(
|
||||
nameof(Data),
|
||||
typeof(object),
|
||||
typeof(BindingProxy)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace ShrlAlgoToolkit.Mvvm.Attach;
|
||||
|
||||
public class TextSearchAssist
|
||||
{
|
||||
// Using a DependencyProperty as the backing store for IsTextMatch. This enables animation, styling, binding, etc...
|
||||
public static readonly DependencyProperty IsTextMatchProperty = DependencyProperty.RegisterAttached(
|
||||
"IsTextMatch",
|
||||
typeof(bool),
|
||||
typeof(TextSearchAssist),
|
||||
new UIPropertyMetadata(false)
|
||||
);
|
||||
|
||||
// Using a DependencyProperty as the backing store for SearchValue. This enables animation, styling, binding, etc...
|
||||
public static readonly DependencyProperty SearchValueProperty = DependencyProperty.RegisterAttached(
|
||||
"SearchValue",
|
||||
typeof(string),
|
||||
typeof(TextSearchAssist),
|
||||
new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.Inherits)
|
||||
);
|
||||
|
||||
public static bool GetIsTextMatch(DependencyObject obj)
|
||||
{
|
||||
return (bool)obj.GetValue(IsTextMatchProperty);
|
||||
}
|
||||
|
||||
public static string GetSearchValue(DependencyObject obj)
|
||||
{
|
||||
return (string)obj.GetValue(SearchValueProperty);
|
||||
}
|
||||
|
||||
public static void SetIsTextMatch(DependencyObject obj, bool value)
|
||||
{
|
||||
obj.SetValue(IsTextMatchProperty, value);
|
||||
}
|
||||
|
||||
public static void SetSearchValue(DependencyObject obj, string value)
|
||||
{
|
||||
obj.SetValue(SearchValueProperty, value);
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using Microsoft.Xaml.Behaviors;
|
||||
|
||||
namespace ShrlAlgoToolkit.Mvvm.Behaviors;
|
||||
|
||||
//<Windows x:Class="WpfApplication6.Window1"
|
||||
// xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
// xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
// xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
|
||||
// xmlns:local="clr-namespace:WpfApplication6"
|
||||
// Title="Window1" Height="300" Width="300">
|
||||
// <i:Interaction.Behaviors>
|
||||
// <local:CloseByButtonBehavior CloseButton="{Binding ElementName=closeButton}"/>
|
||||
// </i:Interaction.Behaviors>
|
||||
// <Grid>
|
||||
// <Button Name="closeButton">CloseTrigger</Button>
|
||||
// </Grid>
|
||||
//</Windows>
|
||||
/// <summary>
|
||||
/// 通过按钮点击,关闭窗口
|
||||
/// </summary>
|
||||
public class CloseByButtonBehavior : Behavior<Window>
|
||||
{
|
||||
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
|
||||
nameof(Command),
|
||||
typeof(ICommand),
|
||||
typeof(CloseByButtonBehavior)
|
||||
);
|
||||
|
||||
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register(
|
||||
nameof(CommandParameter),
|
||||
typeof(object),
|
||||
typeof(CloseByButtonBehavior)
|
||||
);
|
||||
|
||||
public static readonly DependencyProperty CloseButtonProperty = DependencyProperty.Register(
|
||||
nameof(CloseButton),
|
||||
typeof(Button),
|
||||
typeof(CloseByButtonBehavior),
|
||||
new FrameworkPropertyMetadata(null, OnCloseButtonChanged)
|
||||
);
|
||||
|
||||
public ICommand Command
|
||||
{
|
||||
get => (ICommand)GetValue(CommandProperty);
|
||||
set => SetValue(CommandProperty, value);
|
||||
}
|
||||
|
||||
public object CommandParameter
|
||||
{
|
||||
get => GetValue(CommandParameterProperty);
|
||||
set => SetValue(CommandParameterProperty, value);
|
||||
}
|
||||
|
||||
public Button CloseButton
|
||||
{
|
||||
get => (Button)GetValue(CloseButtonProperty);
|
||||
set => SetValue(CloseButtonProperty, value);
|
||||
}
|
||||
|
||||
private static void OnCloseButtonChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var window = ((CloseByButtonBehavior)d).AssociatedObject;
|
||||
((Button)e.NewValue).Click += (s, e1) =>
|
||||
{
|
||||
var command = ((CloseByButtonBehavior)d).Command;
|
||||
var commandParameter = ((CloseByButtonBehavior)d).CommandParameter;
|
||||
command?.Execute(commandParameter);
|
||||
window.Close();
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
using System.Windows;
|
||||
using Microsoft.Xaml.Behaviors;
|
||||
|
||||
namespace ShrlAlgoToolkit.Mvvm.Behaviors;
|
||||
|
||||
/// <summary>
|
||||
/// 通过设置属性值,关闭窗口
|
||||
/// </summary>
|
||||
/// <example>
|
||||
/// <c><i:Interaction.Behaviors>
|
||||
/// <behavior:CloseByPropertyBehavior CloseTrigger = "{Binding CloseTrigger}" />
|
||||
/// </ i:Interaction.Behaviors>
|
||||
/// </c>
|
||||
/// </example>
|
||||
public class CloseByPropertyBehavior : Behavior<Window>
|
||||
{
|
||||
public static readonly DependencyProperty CloseTriggerProperty = DependencyProperty.Register(
|
||||
nameof(CloseTrigger),
|
||||
typeof(bool),
|
||||
typeof(CloseByPropertyBehavior),
|
||||
new PropertyMetadata(false, OnCloseTriggerChanged)
|
||||
);
|
||||
|
||||
public bool CloseTrigger
|
||||
{
|
||||
get => (bool)GetValue(CloseTriggerProperty);
|
||||
set => SetValue(CloseTriggerProperty, value);
|
||||
}
|
||||
|
||||
private static void OnCloseTriggerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if (d is CloseByPropertyBehavior behavior)
|
||||
{
|
||||
behavior.OnCloseTriggerChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnCloseTriggerChanged()
|
||||
{
|
||||
// when closetrigger is true, close the window
|
||||
if (CloseTrigger)
|
||||
{
|
||||
AssociatedObject.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace ShrlAlgoToolkit.Mvvm.Behaviors;
|
||||
|
||||
/// <summary>
|
||||
/// 关闭窗口
|
||||
/// </summary>
|
||||
/// <example><Button Content = "Save" Command="{Binding SaveCommand}"></example>
|
||||
/// <i:Interaction.Triggers>
|
||||
/// <i:EventTrigger EventName = "Click" >
|
||||
/// < i:CallMethodAction MethodName = "CloseTrigger" TargetObject="{Binding RelativeSource={RelativeSource IsMultiSelect=FindAncestor, AncestorType=Windows}}" />
|
||||
/// </i:EventTrigger >
|
||||
/// </i:Interaction.Triggers >
|
||||
///</Button>
|
||||
|
||||
public class CloseOnClickBehaviour : Microsoft.Xaml.Behaviors.Behavior<FrameworkElement>
|
||||
{
|
||||
public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached(
|
||||
"IsEnabled",
|
||||
typeof(bool),
|
||||
typeof(CloseOnClickBehaviour),
|
||||
new PropertyMetadata(false, OnIsEnabledChanged)
|
||||
);
|
||||
|
||||
public static bool GetIsEnabled(DependencyObject obj)
|
||||
{
|
||||
return (bool)obj.GetValue(IsEnabledProperty);
|
||||
}
|
||||
|
||||
public static void SetIsEnabled(DependencyObject obj, bool value)
|
||||
{
|
||||
obj.SetValue(IsEnabledProperty, value);
|
||||
}
|
||||
|
||||
private static void OnIsEnabledChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs args)
|
||||
{
|
||||
if (dpo is not Button button)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var oldValue = (bool)args.OldValue;
|
||||
var newValue = (bool)args.NewValue;
|
||||
|
||||
if (!oldValue && newValue)
|
||||
{
|
||||
button.Click += OnClick;
|
||||
}
|
||||
else if (oldValue && !newValue)
|
||||
{
|
||||
button.PreviewMouseLeftButtonDown -= OnClick;
|
||||
}
|
||||
}
|
||||
|
||||
private static void OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (sender is not Button button)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var win = Window.GetWindow(button);
|
||||
|
||||
win?.Close();
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
using System.Windows;
|
||||
using Microsoft.Xaml.Behaviors;
|
||||
|
||||
namespace ShrlAlgoToolkit.Mvvm.Behaviors;
|
||||
|
||||
/// <summary>
|
||||
/// <20><>ק<EFBFBD>ļ<EFBFBD><C4BC>¼<EFBFBD>
|
||||
/// </summary>
|
||||
/// <example>DropFileBehaviors Data="{Binding FileNames,Mode=OneWayToSource}"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD>ԣ<EFBFBD>ͨ<EFBFBD><CDA8>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>Mvvm</example>
|
||||
/// <remarks><3E><><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>AllowDrop<6F><70><EFBFBD><EFBFBD><EFBFBD>ϲ㸲<CFB2><E3B8B2>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Grid<69><64><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><C3B4>ڵ<EFBFBD>DropEnter<65><72>DropLeave(EventTrigger)<29>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD>Drop<6F>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(ChangePropertyAction)</remarks>
|
||||
class DropFileBehavior : Behavior<FrameworkElement>
|
||||
{
|
||||
public string[] Data
|
||||
{
|
||||
get { return (string[])GetValue(DataProperty); }
|
||||
set { SetValue(DataProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty DataProperty =
|
||||
DependencyProperty.Register(nameof(Data), typeof(string[]), typeof(DropFileBehavior), new PropertyMetadata(null));
|
||||
|
||||
|
||||
protected override void OnAttached()
|
||||
{
|
||||
AssociatedObject.AllowDrop = true;
|
||||
AssociatedObject.Drop += DropHandler;
|
||||
}
|
||||
|
||||
private void DropHandler(object sender, DragEventArgs e)
|
||||
{
|
||||
if (e.Data.GetDataPresent(DataFormats.FileDrop))
|
||||
{
|
||||
Data = (string[])e.Data.GetData(DataFormats.FileDrop);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnDetaching()
|
||||
{
|
||||
AssociatedObject.Drop -= DropHandler;
|
||||
}
|
||||
protected override void OnChanged()
|
||||
{
|
||||
base.OnChanged();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace ShrlAlgoToolkit.Mvvm.Behaviors;
|
||||
|
||||
public interface IValidationExceptionHandler
|
||||
{
|
||||
bool IsValid { get; set; }
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using Microsoft.Xaml.Behaviors;
|
||||
|
||||
namespace ShrlAlgoToolkit.Mvvm.Behaviors;
|
||||
|
||||
/// <summary>
|
||||
/// 在xaml的资源中声明,viewModel继承IValidationExceptionHandler,通过IsValid判断能否执行
|
||||
/// </summary>
|
||||
public class ValidationRuleBehavior : Behavior<FrameworkElement>
|
||||
{
|
||||
private int errorCount;
|
||||
|
||||
protected override void OnAttached()
|
||||
{
|
||||
base.OnAttached();
|
||||
AssociatedObject.AddHandler(Validation.ErrorEvent, new EventHandler<ValidationErrorEventArgs>(OnValidationError));
|
||||
}
|
||||
|
||||
protected override void OnDetaching()
|
||||
{
|
||||
AssociatedObject.RemoveHandler(Validation.ErrorEvent, new EventHandler<ValidationErrorEventArgs>(OnValidationError));
|
||||
}
|
||||
|
||||
private void OnValidationError(object sender, ValidationErrorEventArgs e)
|
||||
{
|
||||
if (AssociatedObject.DataContext is IValidationExceptionHandler handler)
|
||||
{
|
||||
if (e.OriginalSource is UIElement elem)
|
||||
{
|
||||
if (e.Action == ValidationErrorEventAction.Added)
|
||||
{
|
||||
errorCount++;
|
||||
}
|
||||
else if (e.Action == ValidationErrorEventAction.Removed)
|
||||
{
|
||||
errorCount--;
|
||||
}
|
||||
}
|
||||
|
||||
handler.IsValid = errorCount == 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using Microsoft.Xaml.Behaviors;
|
||||
|
||||
namespace ShrlAlgoToolkit.Mvvm.Behaviors;
|
||||
|
||||
//<Window x:Class="WpfApplication6.Window1"
|
||||
// xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
// xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
// xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
|
||||
// xmlns:local="clr-namespace:WpfApplication6"
|
||||
// Title="Window1" Height="300" Width="300">
|
||||
// <i:Interaction.Behaviors>
|
||||
// <local:WindowCloseByButtonBehavior CloseButton="{Binding ElementName=closeButton}"/>
|
||||
// </i:Interaction.Behaviors>
|
||||
// <Grid>
|
||||
// <Button Name="closeButton">CloseTrigger</Button>
|
||||
// </Grid>
|
||||
//</Window>
|
||||
/// <summary>
|
||||
/// 通过按钮点击,关闭窗口
|
||||
/// </summary>
|
||||
public class WindowCloseByButtonBehavior
|
||||
: Behavior<Window>
|
||||
{
|
||||
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
|
||||
nameof(Command),
|
||||
typeof(ICommand),
|
||||
typeof(WindowCloseByButtonBehavior)
|
||||
);
|
||||
|
||||
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register(
|
||||
nameof(CommandParameter),
|
||||
typeof(object),
|
||||
typeof(WindowCloseByButtonBehavior)
|
||||
);
|
||||
|
||||
public static readonly DependencyProperty CloseButtonProperty = DependencyProperty.Register(
|
||||
nameof(CloseButton),
|
||||
typeof(Button),
|
||||
typeof(WindowCloseByButtonBehavior),
|
||||
new FrameworkPropertyMetadata(null, OnCloseButtonChanged)
|
||||
);
|
||||
|
||||
public ICommand Command
|
||||
{
|
||||
get => (ICommand)GetValue(CommandProperty);
|
||||
set => SetValue(CommandProperty, value);
|
||||
}
|
||||
|
||||
public object CommandParameter
|
||||
{
|
||||
get => GetValue(CommandParameterProperty);
|
||||
set => SetValue(CommandParameterProperty, value);
|
||||
}
|
||||
|
||||
public Button CloseButton
|
||||
{
|
||||
get => (Button)GetValue(CloseButtonProperty);
|
||||
set => SetValue(CloseButtonProperty, value);
|
||||
}
|
||||
|
||||
private static void OnCloseButtonChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var window = ((WindowCloseByButtonBehavior)d).AssociatedObject;
|
||||
((Button)e.NewValue).Click += (s, e1) =>
|
||||
{
|
||||
var command = ((WindowCloseByButtonBehavior)d).Command;
|
||||
var commandParameter = ((WindowCloseByButtonBehavior)d).CommandParameter;
|
||||
command?.Execute(commandParameter);
|
||||
window.Close();
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user