原文:
WPF PrismDialog PopupWindowAction使用MetroWindow
Window1ViewModel
Window1DialogModel
Window1
MetroPopupWindowAction
View Code
TestWindowViewModel
本示例必须在prism5.0版本以上
PopupWindowAction如何使用MetroWindow?
public class Window1ViewModel:BindableBase,IInteractionRequestAware { #region Properties private string str; public string Str { get { return str; } set { SetProperty(ref str, value); } } #endregion /// <summary> /// Cancel /// </summary> private DelegateCommand<Window> clickCommand; public DelegateCommand<Window> ClickCommand { get { if (clickCommand == null) clickCommand = new DelegateCommand<Window>(Click); return clickCommand; } } private INotification notification; public INotification Notification { get { return notification; } set { SetProperty(ref notification,value); } } public Action FinishInteraction { get; set; } private void Click(Window window) { var _notification = (Window1DialogModel)Notification; _notification.Str = this.str; _notification.Confirmed = true; FinishInteraction?.Invoke(); } }
public class Window1DialogModel : IConfirmation { public string Str { get; set; } //Interface Content public bool Confirmed { get ; set ; } public string Title { get ; set ; } public object Content { get; set; } }
<UserControl x:Class="Pfu.ScanSnap.Home.UI.SshUIUserData.Views.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:MetroControls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" xmlns:prism="http://www.codeplex.com/prism" xmlns:prop="clr-namespace:Pfu.ScanSnap.Home.UI.SshUIUserData.Properties" xmlns:util ="clr-namespace:Pfu.ScanSnap.Home.UI.SshUIUserData.Util" prism:ViewModelLocator.AutoWireViewModel="True" mc:Ignorable="d" BorderBrush="#FF666666" BorderThickness="1" Width="500" Height="300"> <UserControl.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/SshUIUserData;component/Styles/ResourceDictionary.xaml"/> <ResourceDictionary Source="/SshUIUserData;component/Styles/BaseStyles.xaml" /> <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.Buttons.xaml" /> <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </UserControl.Resources> <Grid> <StackPanel Orientation="Vertical" VerticalAlignment="Center"> <TextBox Text="{Binding Str}"/> <Button Content="Click" Command="{Binding ClickCommand}" CommandParameter="{Binding ElementName=_window1_}"/> </StackPanel> </Grid> </UserControl>
// 核心块
public class MetroPopupWindowAction:PopupWindowAction { protected override Window CreateWindow() { return new MessageDialogWindow(); } }
<!--Window1 Dialog--> <prism:InteractionRequestTrigger SourceObject="{Binding Window1DialogRequest}"> <util:MetroPopupWindowAction IsModal="True" CenterOverAssociatedObject="True"> <util:MetroPopupWindowAction.WindowContent> <local:Window1/> </util:MetroPopupWindowAction.WindowContent> </util:MetroPopupWindowAction> </prism:InteractionRequestTrigger>
private void OK() { Window1DialogRequest.Raise(new Window1DialogModel { Str = $"6666", Title = "Window1" }, _ => { MessageBox.Show(_.Str); }); } #endregion #region NotificationRequest public InteractionRequest<Window1DialogModel> Window1DialogRequest { get; set; } #endregion public ExportMainWindowViewModel() { Window1DialogRequest = new InteractionRequest<Window1DialogModel>(); }