功能完善
This commit is contained in:
38
NeuWPF/NeoUI/Controls/Modal/Modal.cs
Normal file
38
NeuWPF/NeoUI/Controls/Modal/Modal.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
namespace NeumUI.Controls;
|
||||
public static class Modal
|
||||
{
|
||||
/// <summary>
|
||||
/// 显示一个确认对话框
|
||||
/// </summary>
|
||||
/// <param name="owner">父窗口</param>
|
||||
/// <param name="title">标题</param>
|
||||
/// <param name="message">信息</param>
|
||||
/// <param name="onOkAsync">点击确定按钮时执行的异步委托</param>
|
||||
/// <returns>如果用户点击OK并异步操作成功,则返回true,否则返回false</returns>
|
||||
public static bool? Confirm(Window owner, string title, string message, Func<Task<bool>>? onOkAsync = null)
|
||||
{
|
||||
var modal = new ModalWindow(title, message)
|
||||
{
|
||||
Owner = owner,
|
||||
OnOkAsync = onOkAsync
|
||||
};
|
||||
return modal.ShowDialog();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示一个信息对话框
|
||||
/// </summary>
|
||||
/// <param name="owner">父窗口</param>
|
||||
/// <param name="title">标题</param>
|
||||
/// <param name="message">信息</param>
|
||||
public static void Info(Window owner, string title, string message)
|
||||
{
|
||||
var modal = new ModalWindow(title, message)
|
||||
{
|
||||
Owner = owner
|
||||
};
|
||||
// 隐藏取消按钮
|
||||
modal.CancelButton.Visibility = Visibility.Collapsed;
|
||||
modal.ShowDialog();
|
||||
}
|
||||
}
|
||||
100
NeuWPF/NeoUI/Controls/Modal/ModalWindow.xaml
Normal file
100
NeuWPF/NeoUI/Controls/Modal/ModalWindow.xaml
Normal file
@@ -0,0 +1,100 @@
|
||||
<Window
|
||||
AllowsTransparency="True"
|
||||
Background="Transparent"
|
||||
Height="220"
|
||||
Title="Modal"
|
||||
Width="400"
|
||||
WindowStartupLocation="CenterOwner"
|
||||
WindowStyle="None"
|
||||
mc:Ignorable="d"
|
||||
x:Class="NeumUI.Controls.ModalWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:local="clr-namespace:NeumUI.Controls"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||
<Window.Resources>
|
||||
<Style TargetType="Button" x:Key="ModalButtonStyle">
|
||||
<Setter Property="Padding" Value="10,5" />
|
||||
<Setter Property="MinWidth" Value="70" />
|
||||
<Setter Property="Background" Value="#F0F0F0" />
|
||||
<Setter Property="BorderBrush" Value="#D9D9D9" />
|
||||
<Setter Property="BorderThickness" Value="1" />
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="Button">
|
||||
<Border
|
||||
Background="{TemplateBinding Background}"
|
||||
BorderBrush="{TemplateBinding BorderBrush}"
|
||||
BorderThickness="{TemplateBinding BorderThickness}"
|
||||
CornerRadius="4">
|
||||
<ContentPresenter
|
||||
HorizontalAlignment="Center"
|
||||
Margin="{TemplateBinding Padding}"
|
||||
VerticalAlignment="Center" />
|
||||
</Border>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
<Style.Triggers>
|
||||
<Trigger Property="IsMouseOver" Value="True">
|
||||
<Setter Property="Background" Value="#E0E0E0" />
|
||||
</Trigger>
|
||||
<Trigger Property="IsPressed" Value="True">
|
||||
<Setter Property="Background" Value="#D0D0D0" />
|
||||
</Trigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</Window.Resources>
|
||||
<Border
|
||||
Background="White"
|
||||
BorderBrush="#D9D9D9"
|
||||
BorderThickness="1"
|
||||
CornerRadius="8">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="*" />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<!-- Title -->
|
||||
<TextBlock
|
||||
FontSize="16"
|
||||
FontWeight="SemiBold"
|
||||
Grid.Row="0"
|
||||
Margin="15,10"
|
||||
Text="Dialog Title"
|
||||
x:Name="TitleTextBlock" />
|
||||
|
||||
<!-- Content -->
|
||||
<TextBlock
|
||||
Grid.Row="1"
|
||||
Margin="15,10"
|
||||
Text="This is the modal content."
|
||||
TextWrapping="Wrap"
|
||||
VerticalAlignment="Center"
|
||||
x:Name="MessageTextBlock" />
|
||||
|
||||
<!-- Buttons -->
|
||||
<StackPanel
|
||||
Grid.Row="2"
|
||||
HorizontalAlignment="Right"
|
||||
Margin="15,10"
|
||||
Orientation="Horizontal">
|
||||
<Button
|
||||
Click="CancelButton_Click"
|
||||
Content="Cancel"
|
||||
Style="{StaticResource ModalButtonStyle}"
|
||||
x:Name="CancelButton" />
|
||||
<Button
|
||||
Click="OkButton_Click"
|
||||
Content="OK"
|
||||
IsDefault="True"
|
||||
Margin="10,0,0,0"
|
||||
Style="{StaticResource ModalButtonStyle}"
|
||||
x:Name="OkButton" />
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Border>
|
||||
</Window>
|
||||
47
NeuWPF/NeoUI/Controls/Modal/ModalWindow.xaml.cs
Normal file
47
NeuWPF/NeoUI/Controls/Modal/ModalWindow.xaml.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
namespace NeumUI.Controls;
|
||||
/// <summary>
|
||||
/// ModalWindow.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public partial class ModalWindow : Window
|
||||
{
|
||||
// 用于异步操作
|
||||
public Func<Task<bool>>? OnOkAsync { get; set; }
|
||||
|
||||
public ModalWindow(string title, string message)
|
||||
{
|
||||
InitializeComponent();
|
||||
TitleTextBlock.Text = title;
|
||||
MessageTextBlock.Text = message;
|
||||
}
|
||||
|
||||
private async void OkButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (OnOkAsync != null)
|
||||
{
|
||||
OkButton.IsEnabled = false;
|
||||
OkButton.Content = "Loading..."; // 模拟 Ant Design 的加载状态
|
||||
|
||||
var result = await OnOkAsync();
|
||||
if (result)
|
||||
{
|
||||
this.DialogResult = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 恢复按钮状态,停留在对话框
|
||||
OkButton.IsEnabled = true;
|
||||
OkButton.Content = "OK";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.DialogResult = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void CancelButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
this.DialogResult = false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user