RadWindow.Confirm中的DialogResult()

时间:2014-05-09 08:46:25

标签: c# wpf telerik messagebox radwindow

我在我的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
    //...

如何实现这一目标?

2 个答案:

答案 0 :(得分: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);
                        }
                    });

答案 1 :(得分: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 处理函数)。