从ViewModel和Wait for DialogResult打开xaml窗口的最佳方法是什么?

时间:2011-05-27 08:05:12

标签: c# wpf c#-4.0 binding

Currenlty,我正在使用如下。

在xaml中,

<Button Content="X" Width="33" Height="16" Padding="1,-2,1,0"  
 Command="{Binding ElementName=UserControlName, Path=DataContext.DenyCommand}"
   <Button.CommandParameter>
     <wpfext:UICommandParameter UICommandCallerCallback="{Binding ElementName=UserControlName, Path=UIDenyCallBackCommand}"/>
   </Button.CommandParameter>
</Button>

在xaml.cs中,

 public UICommandCallerCallback UIDenyCallBackCommand
        {
            get;
            private set;
        }
public UserControlName()
        {

            this.UIDenyCallBackCommand = this.UIAccessDenyCallBack;
            this.InitializeComponent();

        }

 public void UIAccessDenyCallBack(object commandParameter, object callbackData)
        {
            ShowADenyMsgBox();
        }

private void ShowDenyMsgBox()
{
             RightsDenied win = new RightsDenied(); //xaml window
            win.Owner = GetImmediateWindow();
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            win.ShowDialog();
}

在ViewModel.cs中,

internal ViewModel()
        {
         this.DenyCommand= new DenyCommand(this.AccessDeny);
        }
public void AccessDeny(ICommandState commandState)
        {
            commandState.InvokeCallerCallback("AccessDenied");
        }

public CommandCallback DenyCommand
        {
            get;
            private set;
        }

UICommandCallerCallback声明如下。

public delegate void UICommandCallerCallback(object commandParameter, object callbackData);

CommandCallback类如下。

public class CommandCallback:ICommand
    {
        private readonly Action<ICommandState> executeMethod;

        private readonly Func<ICommandState, bool> canExecuteMethod;

        public CommandCallback(Action<ICommandState> executeMethod)
            : this(executeMethod, null)
        {
        }

        public CommandCallback(Action<ICommandState> executeMethod, Func<ICommandState, bool> canExecuteMethod)
        {
            if (executeMethod == null)
            {
                throw new ArgumentNullException("executeMethod");
            }
            this.executeMethod = executeMethod;
            this.canExecuteMethod = canExecuteMethod;
        }  

        public bool CanExecute(object parameter)
        {
            return this.canExecuteMethod != null ? this.canExecuteMethod((ICommandState)parameter) : true;
        }

        public void Execute(object parameter)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException("parameter","CommandCallback parameter cannot be null");
            }
            if (!(parameter is ICommandState))
            {
                throw new ArgumentException("expects a parameter of type ICommandState","parameter");
            }

            ICommandState state = (ICommandState)parameter;
            this.executeMethod.Invoke(state);
        }

        public event EventHandler CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;
            }

            remove
            {
                CommandManager.RequerySuggested -= value;
            }
        }
    }

如果它只是弹出对话框,它工作正常,但我想等待对话框的结果并想继续AccessDeny()函数。例如。

public void AccessDeny(ICommandState commandState)
            {
               1. processs
               2. open xaml window and wait for the dialogresult. (i.e Yes No or Cancel)
               3. Based on the result, continue processing.

            }

这可能是做这项工作流程的最佳方法吗?请指教。感谢。

1 个答案:

答案 0 :(得分:1)

通读this documentation中的用户互动模式