tags:

views:

16

answers:

2

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?

A: 

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();
        }
rerun
+1  A: 

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;
Reed Copsey