更新整理
This commit is contained in:
74
ShrlAlgoToolkit.Mvvm/Behaviors/CloseByButtonBehavior.cs
Normal file
74
ShrlAlgoToolkit.Mvvm/Behaviors/CloseByButtonBehavior.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
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();
|
||||
};
|
||||
}
|
||||
}
|
||||
46
ShrlAlgoToolkit.Mvvm/Behaviors/CloseByPropertyBehavior.cs
Normal file
46
ShrlAlgoToolkit.Mvvm/Behaviors/CloseByPropertyBehavior.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
67
ShrlAlgoToolkit.Mvvm/Behaviors/CloseOnClickBehaviour.cs
Normal file
67
ShrlAlgoToolkit.Mvvm/Behaviors/CloseOnClickBehaviour.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
45
ShrlAlgoToolkit.Mvvm/Behaviors/DropFileBehavior.cs
Normal file
45
ShrlAlgoToolkit.Mvvm/Behaviors/DropFileBehavior.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace ShrlAlgoToolkit.Mvvm.Behaviors;
|
||||
|
||||
public interface IValidationExceptionHandler
|
||||
{
|
||||
bool IsValid { get; set; }
|
||||
}
|
||||
44
ShrlAlgoToolkit.Mvvm/Behaviors/ValidationRuleBehavior.cs
Normal file
44
ShrlAlgoToolkit.Mvvm/Behaviors/ValidationRuleBehavior.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
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