Kind of a WPF and MahApps newb here (tho it's been a pretty intuitive breeze up to this point) so please bear with me.
I've written a simple app in MahApps (very helpful!) that is a basic single window UI with a slew of slider/button controls of various types and simple callbacks in the MainWindow code behind to interact with a serial interface. We wanted to add a simple dialog which would pop up with a textbox with diagnostic info that the user could then copy and paste.
So far, I've made a custom dialog via a separate xaml page and called that DiagnosticDialog. It has the same namespace was the MainWindow. Code posted below for DiagnosticDialog.xaml and .xaml.cs.
<Dialog:CustomDialog Title="Dagnostic Report"
x:Class="MyApp.DiagnosticDialog"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro">
<StackPanel>
<TextBox IsReadOnly="True"
TextWrapping="Wrap"
Margin="20"
Width="250"
Height="150"
HorizontalAlignment="Center"
AcceptsReturn="True"
Foreground="Gray"
Text="This is some sample text in dialog This should show up on a separate line "/>
<Button x:Name="Close_Button" Content="Close"/>
</StackPanel>
</Dialog:CustomDialog>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
namespace MyApp
{
public partial class DiagnosticDialog : CustomDialog
{
public DiagnosticDialog()
{
InitializeComponent();
}
}
}
I can even make this appear in the main window in the way I would expect form having read elsewhere:
DiagnosticDialog diag_dialog = new DiagnosticDialog();
await this.ShowMetroDialogAsync(diag_dialog);
I can close the dialog immediate after openning with:
await this.HideMetroDialogAsync(diag_dialog);
Once open and everything is as expected, how in the world do I create an event / link a callback / etc. to close the dialog via clicking the close button? In the MainWindow this was more simply done via adding a "Click='SomeFunc'" argument to the button's declaration in xaml. I'm seeing many methods in searching that relate to DataContext and the MVVM view implementation, but this seems wildly beyond the simple action I'm looking for (much as I'd like to delve into MVVM at another time). Is there any easier way or is this simply a fact of life for applications which will manage multiple windows and dialogs?
Any help is very appreciated, thanks!
- Ryan
I figured this out for those reading. It was really simple actually. I just needed to add a "click" callback from the close button of my custom dialog window to a callback method of my MainWindow class from within the same scope as my MainWindow instance. This code snippet looks like:
diag_dialog = new DiagnosticDialog();
diag_dialog.Close_Button.Click += Close_Dialog;
await this.ShowMetroDialogAsync(diag_dialog);
. . .
private async void Close_Dialog(object sender, RoutedEventArgs e)
{
await this.HideMetroDialogAsync(diag_dialog);
}
Most helpful comment
I figured this out for those reading. It was really simple actually. I just needed to add a "click" callback from the close button of my custom dialog window to a callback method of my MainWindow class from within the same scope as my MainWindow instance. This code snippet looks like: