更新整理
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();
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user