Reputation: 105
I want to convert the following methode from winform to WPF
private void btnOK_Click(object sender, System.EventArgs e)
{
if (!EnterNewSettings())
DialogResult = DialogResult.None;
}
I've done this, but does'nt work.
private void btnOK_Click(object sender, System.EventArgs e)
{
if (!EnterNewSettings())
MessageBoxResult result = MessageBoxResult.None;
}
Upvotes: 0
Views: 941
Reputation: 34
private MessageBoxResult isBlaBlaa()
{
Window w = new Window();
w.Tag = MessageBoxResult.Cancel;
Grid grid = new Grid();
grid.Margin = new Thickness(30);
grid.Children.Add(new TextBlock()
{
Text = "Bla blaa",
Margin = new Thickness(0, 0, 0, 20)
});
Button btn;
btn = new Button()
{
VerticalAlignment = System.Windows.VerticalAlignment.Bottom,
HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
Content = "Cancel",
Width = 100,
Height = 30,
};
btn.Click += new RoutedEventHandler((object sender, RoutedEventArgs e) => { w.Tag = MessageBoxResult.Cancel; w.DialogResult = false; });
grid.Children.Add(btn);
btn = new Button()
{
VerticalAlignment = System.Windows.VerticalAlignment.Bottom,
HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
Content = "No",
Width = 100,
Height = 30,
};
btn.Click += new RoutedEventHandler((object sender, RoutedEventArgs e) => { w.Tag = MessageBoxResult.No; w.DialogResult = false; });
grid.Children.Add(btn);
btn = new Button()
{
VerticalAlignment = System.Windows.VerticalAlignment.Bottom,
HorizontalAlignment = System.Windows.HorizontalAlignment.Right,
Content = "Yes",
Width = 100,
Height = 30,
};
btn.Click += new RoutedEventHandler((object sender, RoutedEventArgs e) => { w.Tag = MessageBoxResult.Yes; w.DialogResult = true; });
grid.Children.Add(btn);
w.Content = grid;
w.ShowDialog();
return (MessageBoxResult)w.Tag;
}
Upvotes: 0
Reputation: 33364
Not sure I understand the problem but in WPF Window.DialogResult
is bool?
which means it can be either true
, false
or null
, depending of what is the outcome. If you want to close WPF Window
with success you need to set DialogResult
to true
. When you have Button.IsDefault = "true"
then it will trigger click event on ENTER
but won't close the dialog for you until DialogResult
is set.
private void btnOK_Click(object sender, System.EventArgs e)
{
if (EnterNewSettings()) DialogResult = true;
}
and your DialogResult
is passed as a result of Window.ShowDialog()
if (myDlg.ShowDialog() == true) ....
When you have Button.IsCancel = "true"
then, on ESC
, it will trigger click event and automatically close dialog with DialogResult=False
Upvotes: 1
Reputation: 2629
You have to implement the logic to confirm and close your messagebox your self.
Create a Public Property Succes
Public bool Success {get;set;}
And have this implementation in your ok button if you can to close your form and have it succeed:
private void btnOK_Click(object sender, System.EventArgs e)
{
if (!EnterNewSettings()){
MessageBoxResult result = MessageBoxResult.None;
}else{
Success = true;
Close();
}
}
Then you can Check on the property Success
Could be i'm missing some syntax but i hope you get where it's going to :)
Upvotes: 1