Skip to main content
2 / 2
具体例の追加
joy1192
  • 312
  • 3
  • 9

標準の機能では出来ないはずです。

ConfirmationMessageを継承してデフォルトフォーカス用のプロパティを増やし、
ConfirmationDialogInteractionMessageActionを参考に新しいInteractionMessageAction<FrameworkElement>を継承したTriggerActionを作ることで実現できます。

実装例としては以下のような形になるかと。

public class FocusableConfirmationMessage : ConfirmationMessage
{
    public MessageBoxResult DefaultFocus
    {
        get { return (MessageBoxResult)GetValue(DefaultFocusProperty); }
        set { SetValue(DefaultFocusProperty, value); }
    }

    // Using a DependencyProperty as the backing store for DefaultFocus.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DefaultFocusProperty =
        DependencyProperty.Register("DefaultFocus", typeof(MessageBoxResult), typeof(FocusableConfirmationMessage), new PropertyMetadata(MessageBoxResult.None));

    public FocusableConfirmationMessage() : base()
    {

    }

    public FocusableConfirmationMessage(string text, string caption, MessageBoxImage image, MessageBoxButton button, MessageBoxResult defaultFocus, string messageKey)
        : base(text,caption,image,button,messageKey)
    {
        this.DefaultFocus = defaultFocus;
    }
}

public class FocusableConfirmationDialogInteractionMessageAction : InteractionMessageAction<FrameworkElement>
{
    protected override void InvokeAction(InteractionMessage message)
    {
        var confirmMessage = message as FocusableConfirmationMessage;
        if (confirmMessage != null)
        {
            var result = MessageBox.Show(
                confirmMessage.Text,
                confirmMessage.Caption,
                confirmMessage.Button,
                confirmMessage.Image,
                confirmMessage.DefaultFocus
                );

            if (result == MessageBoxResult.Yes || result == MessageBoxResult.OK)
            {
                confirmMessage.Response = true;
            }
            else if (result == MessageBoxResult.Cancel)
            {
                confirmMessage.Response = null;
            }
            else
            {
                confirmMessage.Response = false;
            }
        }
    }
}
joy1192
  • 312
  • 3
  • 9