I am reading one book which says
Rather than setting the DialogResult by hand after the user clicks a button, you can designate a button as the accept button (by setting IsDefault to true). Clicking that button automatically sets the DialogResult of the window to true. Similarly, you can designate a button as the cancel button (by setting IsCancel to true), in which case clicking it will set the DialogResult to Cancel.
This is the MainWindow:
<Window x:Class="WpfApplicationWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Width="400" Height="400">
<StackPanel>
<Button Name="BtnShowDialogStatus" Click="BtnShowDialogStatus_Click">DIALOG RESULT</Button>
</StackPanel>
</Window>
Code for click event:
private void BtnShowDialogStatus_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(new NewWindow().ShowDialog().ToString());
}
And this is the Dialog box which I am opening on the click event:
<Window x:Class="WpfApplicationWPF.NewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="NewWindow" Height="300" Width="300">
<StackPanel>
<Button Name="BtnDEfault" IsDefault="True" Click="BtnDEfault_Click">DEFAULT BUTTON</Button>
<Button Name="BtnCancel" IsCancel="True" Click="BtnCancel_Click">CANCEL BUTTON</Button>
</StackPanel>
</Window>
This is the code for it:
private void BtnDEfault_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void BtnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
I can see it returning the DialogResult only as false no matter I click the default or cancel button.
IsDefault ties the button to the Enter key, so that pressing the Enter key will fire the Click event. It does not mean that the Yes button will return true for the DialogResult.
Refer to the links.It will clear up things for you
http://blog.wpfwonderland.com/2010/03/22/getting-a-dialogresult-from-a-wpf-window/
http://www.wpftutorial.net/Dialogs.html
Hope it helps...
change your code to
private void BtnDEfault_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
this.Close();
}
private void BtnCancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
this.Close();
}
hope this helps
To my understanding setting IsDefault as true and IsCancel as false only enables you to assign what event should occur i.e. the window will fire close event on 'Escape' key for IsCancel and for Enter key for IsDefault=true.
You will need to set the Dialog result from your button click / command action handlers.
Using net 5 this seems to be all the code needed to open a ShowDialog window, and close it.
From the window you have opened.
<Button Margin="10" IsDefault="True" Click="Ok_OnClick" >OK</Button>
<Button Margin="10" IsCancel="True">Cancel</Button>
private void Ok_OnClick(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
From function to open window.
var requester = new DeleteRequester();// a wpf window
var showDialog = requester.ShowDialog( );
if (showDialog != null && showDialog.Value)
the only reason it is checked for null is to get rid of the blue nag line from re-sharper.
It seems whenever you change the "DialogResult" The window is going to close and the value gets returned. Kind of make sense, why would you change the value if you weren't done.
With what ever your doing with the window, you simply need to close the window to return a false result, or set the DialogResult to true to close the window with a true result.
Simple and basic:
If(ItWorked){DialogResult = true;}// closes window returns true
If(ItsJunk){Close();}// closes window returns false
If(ItsJunk){DialogResult = false;}//closes window returns false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With