2017-02-24 18 views
2

これは私と一緒に遊んでいて、私はそれを理解しているように見えない - 私はここで何か外部の助けが必要です!WPF - PRISM:ボタンをクリックしてCustomPopupWindowを閉じる方法

タイトル、最小化、最大化、および閉じるボタンがないポップアップウィンドウが必要なのは、自分でスタイルを設定し、ポップアップ画面にカスタム閉じるボタンを追加したいからです。

だから、私は今の私どこ得るためにこれらのリンクをたどっ:

https://msdn.microsoft.com/en-us/library/ff921081(v=pandp.40).aspx https://msdn.microsoft.com/en-us/library/gg405494(v=pandp.40).aspx https://www.codeproject.com/Articles/269364/MVVM-PRISM-Modal-Windows-by-using-Interaction-Requ

をしかし、私はまだこれを達成する方法を見つけ出すことはできません。基本ウィンドウには「OK」と「キャンセル」ボタンがありますが、これらはデフォルトであり、私はそれを望んでいません。そのため、私は「カスタムビュー」に行きました。ここ

<Window x:Class="Prototype.Views.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:prism="http://prismlibrary.com/" 
     prism:ViewModelLocator.AutoWireViewModel="True" 
     xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
     xmlns:views="clr-namespace:Prototype.Views" 
     Height="349.146" Width="727.317" 
     WindowState="Maximized"> 
    <Grid> 
     <Button Command="{Binding RaiseCustomPopupViewCommand}">Show Popup Window</Button> 

     <i:Interaction.Triggers> 
      <prism:InteractionRequestTrigger SourceObject="{Binding CustomPopupViewRequest, Mode=OneWay}"> 
       <prism:PopupWindowAction IsModal="True" CenterOverAssociatedObject="True"> 
        <prism:PopupWindowAction.WindowStyle> 
         <Style TargetType="Window"> 
          <Setter Property="ShowInTaskbar" Value="False"/> 
          <Setter Property="WindowStyle" Value="None"/> 
          <Setter Property="ResizeMode" Value="NoResize"/> 
         </Style> 
        </prism:PopupWindowAction.WindowStyle> 
        <prism:PopupWindowAction.WindowContent> 
         <views:CustomPopupView /> 
        </prism:PopupWindowAction.WindowContent> 
       </prism:PopupWindowAction> 
      </prism:InteractionRequestTrigger> 
     </i:Interaction.Triggers> 
    </Grid> 
</Window> 

そしてメインウィンドウのコード:

は、ここに私のメインウィンドウのXAMLで

public class MainWindowViewModel : BindableBase 
{ 
    public InteractionRequest<INotification> CustomPopupViewRequest { get; private set; } 

    public MainWindowViewModel() 
    { 
     CustomPopupViewRequest = new InteractionRequest<INotification>(); 
    } 

    public DelegateCommand RaiseCustomPopupViewCommand => new DelegateCommand(RaiseCustomPopupView, CanRaiseCustomPopupView); 

    public string InteractionResultMessage { get; private set; } 

    private void RaiseCustomPopupView() 
    { 
     InteractionResultMessage = ""; 
     CustomPopupViewRequest.Raise(new Notification { Content = "Message for the CustomPopupView", Title = "Custom Popup" }); 
    } 

    private bool CanRaiseCustomPopupView() 
    { 
     return true; 
    } 
} 

InteractionRequestTriggerのSourceObjectがユーザーコントロールです。ここで

はそれがXAMLだ次のとおりです。

<UserControl x:Class="Prototype.Views.CustomPopupView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:prism="http://prismlibrary.com/" 
      prism:ViewModelLocator.AutoWireViewModel="True" 
      xmlns:local="clr-namespace:Prototype.Views" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300" 
      MinWidth="300" MinHeight="100"> 
    <StackPanel Orientation="Vertical" Background="Gray"> 
     <Button DockPanel.Dock="Right" 
       Content="Close"/> 
    </StackPanel> 
</UserControl> 

あなたが見ることができるように、私は、ユーザーコントロールの「閉じる」ボタンがあります。

私はコマンドを使用しようとしましたが、それでも私はウィンドウにアクセスできないか、ウィンドウを閉じるために何かを実行できません。

プリズムコマンドなど何か気付いていないことがありますか?

ボタンでウィンドウを閉じることはできますか?

ご協力いただきますようお願い申し上げます。 :)

+0

あなた自身の問題を解決してくれてうれしいです。 FYI、Prismの最新ドキュメントはhttp://prismlibrary.readthedocs.io/en/latest/でご覧いただけます。このために日付のMSDN記事を使用する必要はありません。 –

答えて

1

私はちょうど私自身でこれを解決することができました:)。

私は違った考えをしていましたが、実際には欲しかったのはCancelCommandでした。

これは、これをUserControlで実装する方法で、これ以外の変更はありません。上記のようにすべてはまだ同じであるが、それはViewModelにだでCustomPopupは今、次のようにあります

public class CustomPopupViewModel : BindableBase, IInteractionRequestAware 
{ 
    public CustomPopupViewModel() 
    { 
     CancelCommand = new DelegateCommand(CancelInteraction); 
    } 

    private CustomPopupSelectionNotification notification; 

    public INotification Notification 
    { 
     get 
     { 
      return this.notification; 
     } 
     set 
     { 
      if (value is CustomPopupSelectionNotification) 
      { 
       this.notification = value as CustomPopupSelectionNotification; 
       this.OnPropertyChanged(() => this.Notification); 
      } 
     } 
    } 

    public Action FinishInteraction { get; set; } 

    public System.Windows.Input.ICommand CancelCommand { get; private set; } 

    public void CancelInteraction() 
    { 
     if (notification != null) 
     { 
      notification.SelectedItem = null; 
      notification.Confirmed = false; 
     } 

     FinishInteraction(); 
    } 
} 

あなたはまた、我々はCustomPopupSelectionNotificationというクラスを持っていることがわかります。代わりにcloseにそれをしようとすると、

public class CustomPopupSelectionNotification : Confirmation 
{ 
    public CustomPopupSelectionNotification() 
    { 
     Items = new List<string>(); 
     SelectedItem = null; 
    } 

    public CustomPopupSelectionNotification(IEnumerable<string> items) : this() 
    { 
     foreach (string item in items) 
     { 
      Items.Add(item); 
     } 
    } 

    public IList<string> Items { get; private set; } 

    public string SelectedItem { get; set; } 
} 

だから、要するに、私はちょうどcancellingポップアップ:

は、ここではコードですです。

次に、私のUserControlの "Close"ボタンにCancelCommandというコマンドを追加しました。

関連する問題