Skip to content

Commit

Permalink
ModernDialog returns proper DialogResult and MessageBoxResult values …
Browse files Browse the repository at this point in the history
…(finally...)

* ModernDialog.DialogResult now properly set based on MessageBoxResult
* new ModernDialog.MessageBoxResult property
ModernDialog.ShowMessage now includes optional Window owner parameter
  • Loading branch information
kozw_cp authored and kozw_cp committed Aug 9, 2014
1 parent e0a84af commit 5115fc9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@
<StackPanel HorizontalAlignment="Left">
<TextBlock Text="MODERNDIALOG" Style="{StaticResource Heading2}" Margin="0,0,0,8" />

<Button Content="common dialog" Margin="0,0,0,32" Click="CommonDialog_Click"/>
<Button Content="common dialog" Margin="0,0,0,8" HorizontalAlignment="Left" Click="CommonDialog_Click"/>
<TextBlock>
<Run Text="Dialog result:" />
<Run x:Name="dialogResult" FontWeight="Bold" />
</TextBlock>
<TextBlock Margin="0,0,0,32">
<Run Text="MessageBox result:" />
<Run x:Name="dialogMessageBoxResult" FontWeight="Bold" />
</TextBlock>

<TextBlock Text="MESSAGEBOX" Style="{StaticResource Heading2}" Margin="0,0,0,8" />
<StackPanel Margin="0,0,0,8">
<RadioButton x:Name="ok" Content="ok" IsChecked="True" Margin="0,0,0,2" />
Expand All @@ -19,9 +28,8 @@

<Button Content="message dialog" Click="MessageDialog_Click" Margin="0,0,0,8"/>
<TextBlock>
<Run Text="Dialog result:" />
<Run x:Name="runResult" FontWeight="Bold" />
<Run Text="MessageBox result:" />
<Run x:Name="msgboxResult" FontWeight="Bold" />
</TextBlock>

</StackPanel>
</UserControl>
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ public ControlsModernDialog()

private void CommonDialog_Click(object sender, RoutedEventArgs e)
{
new ModernDialog {
var dlg = new ModernDialog {
Title = "Common dialog",
Content = new LoremIpsum()
}.ShowDialog();
};
dlg.Buttons = new Button[] { dlg.OkButton, dlg.CancelButton};
dlg.ShowDialog();

this.dialogResult.Text = dlg.DialogResult.HasValue ? dlg.DialogResult.ToString() : "<null>";
this.dialogMessageBoxResult.Text = dlg.MessageBoxResult.ToString();
}

private void MessageDialog_Click(object sender, RoutedEventArgs e)
Expand All @@ -45,7 +50,7 @@ private void MessageDialog_Click(object sender, RoutedEventArgs e)

var result = ModernDialog.ShowMessage("This is a simple Modern UI styled message dialog. Do you like it?", "Message Dialog", btn);

this.runResult.Text = result.ToString();
this.msgboxResult.Text = result.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class ModernDialog
private Button noButton;
private Button closeButton;

private MessageBoxResult dialogResult = MessageBoxResult.None;
private MessageBoxResult messageBoxResult = MessageBoxResult.None;

/// <summary>
/// Initializes a new instance of the <see cref="ModernDialog"/> class.
Expand All @@ -46,7 +46,18 @@ public ModernDialog()
this.closeCommand = new RelayCommand(o => {
var result = o as MessageBoxResult?;
if (result.HasValue) {
this.dialogResult = result.Value;
this.messageBoxResult = result.Value;

// sets the Window.DialogResult as well
if (result.Value == MessageBoxResult.OK || result.Value == MessageBoxResult.Yes) {
this.DialogResult = true;
}
else if (result.Value == MessageBoxResult.Cancel || result.Value == MessageBoxResult.No){
this.DialogResult = false;
}
else{
this.DialogResult = null;
}
}
Close();
});
Expand Down Expand Up @@ -169,14 +180,26 @@ public IEnumerable<Button> Buttons
set { SetValue(ButtonsProperty, value); }
}

/// <summary>
/// Gets the message box result.
/// </summary>
/// <value>
/// The message box result.
/// </value>
public MessageBoxResult MessageBoxResult
{
get { return this.messageBoxResult; }
}

/// <summary>
/// Displays a messagebox.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="title">The title.</param>
/// <param name="button">The button.</param>
/// <param name="owner">The window owning the messagebox. The messagebox will be located at the center of the owner.</param>
/// <returns></returns>
public static MessageBoxResult ShowMessage(string text, string title, MessageBoxButton button)
public static MessageBoxResult ShowMessage(string text, string title, MessageBoxButton button, Window owner = null)
{
var dlg = new ModernDialog {
Title = title,
Expand All @@ -186,10 +209,13 @@ public static MessageBoxResult ShowMessage(string text, string title, MessageBox
MaxHeight = 480,
MaxWidth = 640,
};
if (owner != null) {
dlg.Owner = owner;
}

dlg.Buttons = GetButtons(dlg, button);
dlg.ShowDialog();
return dlg.dialogResult;
return dlg.messageBoxResult;
}

private static IEnumerable<Button> GetButtons(ModernDialog owner, MessageBoxButton button)
Expand Down

0 comments on commit 5115fc9

Please sign in to comment.