2017-02-24 85 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

但我仍然無法弄清楚如何實現這一目標。基本的窗口有「確定」和「取消」按鈕,但這些都是默認設置,我不想這樣做,這就是爲什麼我選擇「自定義視圖」的原因。

這裏是我的主窗口的XAML:

<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> 

這裏,它的主窗口的代碼:

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

很高興看到你解決了你自己的問題。僅供參考,最新的棱鏡文檔可以在這裏找到:http://prismlibrary.readthedocs.io/en/latest/。無需爲此使用過時的MSDN文章。 –

回答

1

無論如何,我只是設法解決這個我自己:)。

我在想它的不同,但我真正想要的是CancelCommand

這就是我們如何在UserControl中實現它,沒有其他更改。一切都還是一樣如上所述,但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類。

下面是它的代碼:

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彈出,而不是試圖close它。

然後我將命令CancelCommand添加到我的UserControl上的「關閉」按鈕。