1

I have a Window with seven buttons; I use it as a menu in a simple game I am working on, but I display it as a dialog. How can I know which button user has pressed, since DialogResult in WPF only offers true, false and null?

2 Answers 2

2

If you're making a custom Window in this way, you don't really need to worry about DialogResult.

You can keep track of this in a property within your Window, and just read the property after the dialog is closed.

 MyDialog window = new MyDialog();
 if (window.ShowDialog() == false)
 {
    // user closed the window...
 }

 var choice = window.CustomPropertyContainingChoice;
Sign up to request clarification or add additional context in comments.

Comments

0

Define your own enum and offer a static method to display the window that return your enum.

The code below does the same thing it is part of a window that allows users to review their changes and accept or cancel. As I only need true and false I used a bool however it would be trivial to change to an enum.

public static bool DisplayChanges(List<INormalizedMessage> LstMessages)
        {
            var retlist = LstMessages.Where(( INormalizedMessage NM ) => { return NM.Status != NormalizedMessageStatus.NoChange; });
            ReviewChanges RC = new ReviewChanges();
            RC.Messages = retlist.ToList();
            RC.ShowDialog();
            return RC.Result;

        }

        private void cmdCancle_Click( object sender, RoutedEventArgs e )
        {
            Result = false;
            Hide();
        }

        private void cmdOK_Click( object sender, RoutedEventArgs e )
        {
            Result = true;
            Hide();
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.