feat: implement buttons in different conditions.
This commit is contained in:
@@ -9,5 +9,9 @@
|
||||
x:Class="Ursa.Demo.Pages.MessageBoxDemo">
|
||||
<StackPanel HorizontalAlignment="Left">
|
||||
<Button Content="Default" Command="{Binding DefaultMessageBoxCommand}"></Button>
|
||||
<Button Content="OK" Command="{Binding OkCommand}" ></Button>
|
||||
<Button Content="OKCancel" Command="{Binding OkCancelCommand}" ></Button>
|
||||
<Button Content="YesNo" Command="{Binding YesNoCommand}" ></Button>
|
||||
<Button Content="YesNoCancel" Command="{Binding YesNoCancelCommand}" ></Button>
|
||||
</StackPanel>
|
||||
</UserControl>
|
||||
|
||||
@@ -9,14 +9,42 @@ namespace Ursa.Demo.ViewModels;
|
||||
public class MessageBoxDemoViewModel: ObservableObject
|
||||
{
|
||||
public ICommand DefaultMessageBoxCommand { get; set; }
|
||||
public ICommand OkCommand { get; set; }
|
||||
public ICommand YesNoCommand { get; set; }
|
||||
public ICommand YesNoCancelCommand { get; set; }
|
||||
public ICommand OkCancelCommand { get; set; }
|
||||
|
||||
public MessageBoxDemoViewModel()
|
||||
{
|
||||
DefaultMessageBoxCommand = new AsyncRelayCommand(OnDefaultMessageAsync);
|
||||
OkCommand = new AsyncRelayCommand(OnOkAsync);
|
||||
YesNoCommand = new AsyncRelayCommand(OnYesNoAsync);
|
||||
YesNoCancelCommand = new AsyncRelayCommand(OnYesNoCancelAsync);
|
||||
OkCancelCommand = new AsyncRelayCommand(OnOkCancelAsync);
|
||||
}
|
||||
|
||||
private async Task OnDefaultMessageAsync()
|
||||
{
|
||||
var result = await MessageBox.ShowAsync("Hello Message Box");
|
||||
}
|
||||
|
||||
private async Task OnOkAsync()
|
||||
{
|
||||
var result = await MessageBox.ShowAsync("Hello Message Box", "Hello", MessageBoxButton.OK);
|
||||
}
|
||||
|
||||
private async Task OnYesNoAsync()
|
||||
{
|
||||
var result = await MessageBox.ShowAsync("Hello Message Box", "Hello", MessageBoxButton.YesNo);
|
||||
}
|
||||
|
||||
private async Task OnYesNoCancelAsync()
|
||||
{
|
||||
var result = await MessageBox.ShowAsync("Hello Message Box", "Hello", MessageBoxButton.YesNoCancel);
|
||||
}
|
||||
|
||||
private async Task OnOkCancelAsync()
|
||||
{
|
||||
var result = await MessageBox.ShowAsync("Hello Message Box", "Hello", MessageBoxButton.OKCancel);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
xmlns:u="https://irihi.tech/ursa">
|
||||
<!-- Add Resources Here -->
|
||||
<ControlTheme x:Key="{x:Type u:MessageBoxWindow}" TargetType="u:MessageBoxWindow">
|
||||
<Setter Property="Title" Value="{x:Null}"></Setter>
|
||||
<Setter Property="Title" Value="{x:Null}" />
|
||||
<Setter Property="Background" Value="{DynamicResource WindowDefaultBackground}" />
|
||||
<Setter Property="TransparencyBackgroundFallback" Value="{DynamicResource WindowDefaultBackground}" />
|
||||
<Setter Property="Foreground" Value="{DynamicResource WindowDefaultForeground}" />
|
||||
@@ -24,13 +24,30 @@
|
||||
<Border Background="{TemplateBinding Background}" IsHitTestVisible="False" />
|
||||
<Panel Margin="{TemplateBinding WindowDecorationMargin}" Background="Transparent" />
|
||||
<Grid RowDefinitions="Auto, *, Auto">
|
||||
<Grid ColumnDefinitions="*, Auto" Grid.Row="0">
|
||||
<TextBlock Grid.Column="0" Text="{TemplateBinding Title}" />
|
||||
<Grid Grid.Row="0" ColumnDefinitions="*, Auto">
|
||||
<TextBlock Grid.Column="0" Text="{TemplateBinding Title}" Margin="8 8 0 0" FontWeight="Bold" FontSize="14" />
|
||||
<!-- A temporary style copied from Semi. Will replace when I get time -->
|
||||
<Button
|
||||
Grid.Column="1"
|
||||
Name="{x:Static u:MessageBoxWindow.PART_CloseButton}"
|
||||
Content="X"
|
||||
/>
|
||||
Grid.Column="1"
|
||||
Margin="0,4,4,0"
|
||||
Background="{DynamicResource CaptionButtonClosePointeroverBackground}"
|
||||
BorderBrush="{DynamicResource CaptionButtonClosePressedBackground}"
|
||||
Theme="{DynamicResource CaptionButton}">
|
||||
<Button.Styles>
|
||||
<Style Selector="Button:pointerover">
|
||||
<Setter Property="Foreground" Value="White" />
|
||||
</Style>
|
||||
<Style Selector="Button:pressed">
|
||||
<Setter Property="Foreground" Value="White" />
|
||||
</Style>
|
||||
</Button.Styles>
|
||||
<PathIcon
|
||||
Width="12"
|
||||
Height="12"
|
||||
Data="{DynamicResource WindowCloseIconGlyph}"
|
||||
Foreground="{Binding $parent[Button].Foreground}" />
|
||||
</Button>
|
||||
</Grid>
|
||||
<ContentPresenter
|
||||
Name="PART_ContentPresenter"
|
||||
@@ -40,11 +57,31 @@
|
||||
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
|
||||
Content="{TemplateBinding Content}"
|
||||
ContentTemplate="{TemplateBinding ContentTemplate}" />
|
||||
<StackPanel Grid.Row="2" Orientation="Horizontal">
|
||||
<Button Name="{x:Static u:MessageBoxWindow.PART_YesButton}" Classes="Primary" Content="Yes" />
|
||||
<Button Name="{x:Static u:MessageBoxWindow.PART_NoButton}" Classes="Danger" Content="No" />
|
||||
<Button Name="{x:Static u:MessageBoxWindow.PART_OKButton}" Classes="Primary" Content="OK" />
|
||||
<Button Name="{x:Static u:MessageBoxWindow.PART_CancelButton}" Classes="Tertiary" Content="Cancel" />
|
||||
<StackPanel
|
||||
Grid.Row="2"
|
||||
Margin="0,0,8,8"
|
||||
HorizontalAlignment="Right"
|
||||
Orientation="Horizontal">
|
||||
<Button
|
||||
Name="{x:Static u:MessageBoxWindow.PART_YesButton}"
|
||||
Margin="8,0,0,0"
|
||||
Classes="Primary"
|
||||
Content="Yes" />
|
||||
<Button
|
||||
Name="{x:Static u:MessageBoxWindow.PART_NoButton}"
|
||||
Margin="8,0,0,0"
|
||||
Classes="Danger"
|
||||
Content="No" />
|
||||
<Button
|
||||
Name="{x:Static u:MessageBoxWindow.PART_OKButton}"
|
||||
Margin="8,0,0,0"
|
||||
Classes="Primary"
|
||||
Content="OK" />
|
||||
<Button
|
||||
Name="{x:Static u:MessageBoxWindow.PART_CancelButton}"
|
||||
Margin="8,0,0,0"
|
||||
Classes="Tertiary"
|
||||
Content="Cancel" />
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Panel>
|
||||
|
||||
@@ -31,4 +31,32 @@ public static class MessageBox
|
||||
return MessageBoxResult.None;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<MessageBoxResult> ShowAsync(string message, string title, MessageBoxButton button)
|
||||
{
|
||||
var messageWindow = new MessageBoxWindow(button)
|
||||
{
|
||||
Content = message,
|
||||
Title = title
|
||||
};
|
||||
var lifetime = Application.Current?.ApplicationLifetime;
|
||||
if (lifetime is IClassicDesktopStyleApplicationLifetime classLifetime)
|
||||
{
|
||||
var main = classLifetime.MainWindow;
|
||||
if (main is null)
|
||||
{
|
||||
messageWindow.Show();
|
||||
return MessageBoxResult.None;
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = await messageWindow.ShowDialog<MessageBoxResult>(main);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return MessageBoxResult.None;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,39 +93,80 @@ public class MessageBoxWindow: Window
|
||||
{
|
||||
_cancelButton.Click += OnCancelButtonClick;
|
||||
}
|
||||
SetButtonVisibility();
|
||||
}
|
||||
|
||||
private void SetButtonVisibility()
|
||||
{
|
||||
if (_buttonConfigs == MessageBoxButton.OK)
|
||||
{
|
||||
if (_closeButton != null) _closeButton.IsVisible = true;
|
||||
if (_yesButton != null) _yesButton.IsVisible = false;
|
||||
if (_noButton != null) _noButton.IsVisible = false;
|
||||
if (_okButton != null) _okButton.IsVisible = true;
|
||||
if (_cancelButton != null) _cancelButton.IsVisible = false;
|
||||
}
|
||||
else if (_buttonConfigs == MessageBoxButton.OKCancel)
|
||||
{
|
||||
if (_closeButton != null) _closeButton.IsVisible = true;
|
||||
if (_yesButton != null) _yesButton.IsVisible = false;
|
||||
if (_noButton != null) _noButton.IsVisible = false;
|
||||
if (_okButton != null) _okButton.IsVisible = true;
|
||||
if (_cancelButton != null) _cancelButton.IsVisible = true;
|
||||
}
|
||||
else if (_buttonConfigs == MessageBoxButton.YesNo)
|
||||
{
|
||||
if (_closeButton != null) _closeButton.IsVisible = false;
|
||||
if (_yesButton != null) _yesButton.IsVisible = true;
|
||||
if (_noButton != null) _noButton.IsVisible = true;
|
||||
if (_okButton != null) _okButton.IsVisible = false;
|
||||
if (_cancelButton != null) _cancelButton.IsVisible = false;
|
||||
}
|
||||
else if (_buttonConfigs == MessageBoxButton.YesNoCancel)
|
||||
{
|
||||
if (_closeButton != null) _closeButton.IsVisible = true;
|
||||
if (_yesButton != null) _yesButton.IsVisible = true;
|
||||
if (_noButton != null) _noButton.IsVisible = true;
|
||||
if (_okButton != null) _okButton.IsVisible = false;
|
||||
if (_cancelButton != null) _cancelButton.IsVisible = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnCloseButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
this.Close(MessageBoxResult.None);
|
||||
if (_buttonConfigs == MessageBoxButton.OK)
|
||||
{
|
||||
Close(MessageBoxResult.OK);
|
||||
}
|
||||
Close(MessageBoxResult.Cancel);
|
||||
}
|
||||
|
||||
private void OnYesButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
this.Close(MessageBoxResult.Yes);
|
||||
Close(MessageBoxResult.Yes);
|
||||
}
|
||||
|
||||
private void OnNoButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
this.Close(MessageBoxResult.No);
|
||||
Close(MessageBoxResult.No);
|
||||
}
|
||||
|
||||
private void OnOKButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
this.Close(MessageBoxResult.OK);
|
||||
Close(MessageBoxResult.OK);
|
||||
}
|
||||
|
||||
private void OnCancelButtonClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
this.Close(MessageBoxResult.Cancel);
|
||||
Close(MessageBoxResult.Cancel);
|
||||
}
|
||||
|
||||
protected override void OnKeyUp(KeyEventArgs e)
|
||||
{
|
||||
base.OnKeyUp(e);
|
||||
if (e.Key == Key.Escape)
|
||||
if (e.Key == Key.Escape && _buttonConfigs == MessageBoxButton.OK)
|
||||
{
|
||||
this.Close(MessageBoxResult.None);
|
||||
Close(MessageBoxResult.OK);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user