0

我在我的 WPF 应用程序中使用radwindow我的自定义confirmbox。现在我需要得到用户在不触发事件的情况下单击的结果。

//代码:

    DialogParameters param = new DialogParameters();
    param.Theme = new Windows8Theme();
    param.OkButtonContent = "Save";
    param.CancelButtonContent = "Discard";
    param.Content = "Do you want to save your unsaved changes?";
    param.Header = "";

    RadWindow.Confirm(param);

有点像,

DialogResult result = MessageBox.Show("Do you want to save changes?", "Confirmation", messageBoxButtons.YesNoCancel);
if(result == DialogResult.Yes)
    //...
else if (result == DialogResult.No)
    //...
else
    //...

如何做到这一点?

4

2 回答 2

1

看看这是否适合你。我对 RadWindow 对象进行了子类化。

public class MyWindow : RadWindow
{
    #region Public Methods

    /// <summary>
    /// Alerts the specified message.
    /// </summary>
    /// <param name="message">The message.</param>
    /// <param name="width">The width.</param>
    public static void Alert(string message, int width = 400 )
    {
        var dialogParams = new DialogParameters
        {
            Content = new TextBlock()
            {
                Text = message,
                Width = width,
                TextWrapping = TextWrapping.Wrap
            },            
            Owner = Application.Current.MainWindow
        };

        RadWindow.Alert(dialogParams);
    }

    /// <summary>
    /// Confirms the specified content.
    /// </summary>
    /// <param name="message">The content.</param>
    /// <param name="closed">The closed.</param>
    /// <param name="width">The width.</param>
    public static void Confirm(string message, EventHandler<WindowClosedEventArgs> closed, int width = 400)
    {
        RadWindow.Confirm(new DialogParameters
        {
            Content = new TextBlock()
            {                   
                Text = message,
                Width = width,                   
                TextWrapping = TextWrapping.Wrap
            },
            Closed = closed,                  
            Owner = Application.Current.MainWindow
        });
    }

    #endregion Public Methods
}

然后像这样打电话...

                MyWindow.Confirm(message,
                    delegate(object windowSender, WindowClosedEventArgs args)
                    {
                        if (args.DialogResult == true)
                        {
                            this.securityViewModel.UndeleteUser(fex.Detail.ExistingDeletedUserId.Value);
                        }
                    });
于 2014-08-08T20:37:50.620 回答
0

RadWindow.Confirm还接受另一个参数,它是一个要在其中执行的处理程序OnClose

我这样解决了:

bool DialogResult = false;
public void ShowConfirmation(string Message){
    DialogParameters param = new DialogParameters();
    param.Content = message;
    param.Owner = App.Current.MainWindow;
    param.Closed += (object sender, WindowClosedEventArgs e) => {
                DialogResult = e.DialogResult == true ? true : false;
            }; 

    RadWindow.Confirm(param);
    return DialogResult
}

e.DialogResult它是一个可为空的布尔值,我将它保存在代码中其他地方定义的变量中(在我的例子DialogResult中,它是在调用 Dialog 的方法之外定义的,因此我可以在方法和 lambda 处理函数中使用它)。

于 2021-01-05T15:37:08.327 回答