Last active
April 13, 2020 21:14
-
-
Save punker76/e1f3b14375d44c3206f259759833713a to your computer and use it in GitHub Desktop.
Custom Dialog sample with result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainWindowViewModel | |
{ | |
private IDialogCoordinator _dialogCoordinator; | |
public MainWindowViewModel(IDialogCoordinator dialogCoordinator) | |
{ | |
_dialogCoordinator = dialogCoordinator; | |
OpenDialogCommand = new ActionCommand(OpenDialog); | |
} | |
private async void OpenDialog() | |
{ | |
var customDialog = new MyCustomDialog(); | |
await _dialogCoordinator.ShowMetroDialogAsync(this, customDialog); | |
var result = await customDialog.WaitForClosingAsync(); | |
await _dialogCoordinator.HideMetroDialogAsync(this, customDialog); | |
} | |
public ICommand OpenDialogCommand { get; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Interaction logic for MyCustomDialog.xaml | |
/// </summary> | |
public partial class MyCustomDialog : CustomDialog | |
{ | |
private TaskCompletionSource<bool> tcs; | |
public MyCustomDialog() | |
: this(null, null) | |
{ | |
} | |
public MyCustomDialog(MetroWindow parentWindow) | |
: this(parentWindow, null) | |
{ | |
} | |
public MyCustomDialog(MetroDialogSettings settings) | |
: this(null, settings) | |
{ | |
} | |
public MyCustomDialog(MetroWindow parentWindow, MetroDialogSettings settings) | |
: base(parentWindow, settings) | |
{ | |
InitializeComponent(); | |
tcs = new TaskCompletionSource<bool>(); | |
} | |
private void PART_AffirmativeButton_OnClick(object sender, RoutedEventArgs e) | |
{ | |
tcs.SetResult(true); | |
} | |
private void PART_NegativeButton_OnClick(object sender, RoutedEventArgs e) | |
{ | |
tcs.SetResult(false); | |
} | |
public Task<bool> WaitForClosingAsync() | |
{ | |
return tcs.Task; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<mah:CustomDialog x:Class="CustomDialogSample.MyCustomDialog" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
d:DesignHeight="450" | |
d:DesignWidth="800" | |
mc:Ignorable="d"> | |
<Grid Margin="0 10 0 0"> | |
<Grid.RowDefinitions> | |
<RowDefinition Height="*" /> | |
<RowDefinition Height="Auto" /> | |
</Grid.RowDefinitions> | |
<Grid> | |
<Grid.RowDefinitions> | |
<RowDefinition Height="Auto" /> | |
<RowDefinition Height="Auto" /> | |
<RowDefinition Height="Auto" /> | |
</Grid.RowDefinitions> | |
<TextBlock Grid.Row="0" Text="Row 0" /> | |
<TextBlock Grid.Row="1" Text="Row 1" /> | |
<TextBlock Grid.Row="2" Text="Row 2" /> | |
</Grid> | |
<StackPanel Grid.Row="1" | |
Margin="0 4" | |
HorizontalAlignment="Right" | |
VerticalAlignment="Bottom" | |
Orientation="Horizontal"> | |
<Button x:Name="PART_AffirmativeButton" | |
MinWidth="80" | |
Margin="0 0 5 0" | |
mah:ControlsHelper.ContentCharacterCasing="Normal" | |
Click="PART_AffirmativeButton_OnClick" | |
Content="Ok" | |
IsDefault="True" | |
Style="{DynamicResource MahApps.Styles.Button.Dialogs.Accent}" /> | |
<Button x:Name="PART_NegativeButton" | |
MinWidth="80" | |
Margin="5 0 5 0" | |
mah:ControlsHelper.ContentCharacterCasing="Normal" | |
Click="PART_NegativeButton_OnClick" | |
Content="Cancel" | |
IsCancel="True" | |
Style="{DynamicResource MahApps.Styles.Button.Dialogs}" /> | |
</StackPanel> | |
</Grid> | |
</mah:CustomDialog> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment