Guest User

Untitled

a guest
Feb 24th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1. <Window x:Class="MyApp.Windows.Views.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:services="clr-namespace:MyApp.Windows.Services"
  7. xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
  8. xmlns:prism="http://prismlibrary.com/"
  9. mc:Ignorable="d"
  10. x:Name="Window"
  11. prism:ViewModelLocator.AutoWireViewModel="True">
  12. <Window.Resources>
  13. <services:OpenFileDialogService x:Key="OpenFileDialogService" Title="Select File" Filter="Text File (*.txt)|*.txt|All Files (*.*)|*.*" DefaultFilterIndex="0" Multiselect="False" />
  14. </Window.Resources>
  15. <i:Interaction.Triggers>
  16. <prism:InteractionRequestTrigger SourceObject="{Binding SelectFileInteractionRequest, Mode=OneWay}">
  17. <services:PopupOpenFileDialogAction DialogService="{StaticResource OpenFileDialogService}" Owner="{Binding ElementName=Window}" />
  18. </prism:InteractionRequestTrigger>
  19. </i:Interaction.Triggers>
  20. <StackPanel Orientation="Vertical">
  21. <TextBox Text="{Binding Filename}"/>
  22. <Button Content="_Select File..." Command="{Binding SelectFileCommand}"/>
  23. </StackPanel>
  24. </Window>
  25.  
  26. namespace MyApp.Windows.ViewModels
  27. {
  28. using System;
  29. using System.Linq;
  30. using System.Windows;
  31. using System.Windows.Input;
  32. using Prism.Mvvm;
  33. using Prism.Commands;
  34. using Prism.Interactivity.InteractionRequest;
  35. using MyApp.Windows.Services;
  36.  
  37. public class MainWindowViewModel : BindableBase
  38. {
  39. private string filename;
  40.  
  41. public MainWindowViewModel()
  42. {
  43. this.SelectFileCommand = new DelegateCommand(this.SelectFile);
  44. this.SelectFileInteractionRequest = new InteractionRequest<OpenFileDialogInteractionContext>();
  45. }
  46.  
  47. public string Filename
  48. {
  49. get => this.filename;
  50. set => this.SetProperty(ref this.filename, value);
  51. }
  52.  
  53. public ICommand SelectFileCommand { get; }
  54.  
  55. public InteractionRequest<OpenFileDialogInteractionContext> SelectFileInteractionRequest { get; }
  56.  
  57. private void SelectFile()
  58. {
  59. this.RaiseSelectFileInteractionRequest();
  60. }
  61.  
  62. private void RaiseSelectFileInteractionRequest()
  63. {
  64. var context = new OpenFileDialogInteractionContext();
  65.  
  66. this.SelectFileInteractionRequest.Raise(
  67. context,
  68. c => this.UpdateFilename(c));
  69. }
  70.  
  71. private void UpdateFilename(OpenFileDialogInteractionContext context)
  72. {
  73. if (context.Confirmed)
  74. {
  75. this.Filename = context.Filenames.First();
  76. }
  77. }
  78. }
  79. }
  80.  
  81. namespace MyApp.Windows.Services
  82. {
  83. using System;
  84. using System.Windows;
  85. using Microsoft.Win32;
  86.  
  87. public class OpenFileDialogService
  88. {
  89. public string Filter { get; set; } = string.Empty;
  90. public string Title { get; set; } = string.Empty;
  91. public int DefaultFilterIndex { get; set; } = 0;
  92. public bool Multiselect { get; set; }
  93.  
  94. public string[] ShowDialog(Window owner)
  95. {
  96. OpenFileDialog dialog = new OpenFileDialog()
  97. {
  98. Title = this.Title,
  99. Filter = this.Filter,
  100. FilterIndex = this.DefaultFilterIndex,
  101. Multiselect = this.Multiselect
  102. };
  103.  
  104. var dialogResult = owner != null ? dialog.ShowDialog(owner) : dialog.ShowDialog();
  105.  
  106. if (!dialogResult.HasValue || !dialogResult.Value)
  107. {
  108. return null;
  109. }
  110.  
  111. return dialog.FileNames;
  112. }
  113.  
  114. public string[] ShowDialog()
  115. {
  116. return this.ShowDialog(null);
  117. }
  118. }
  119. }
  120.  
  121. namespace MyApp.Windows.Services
  122. {
  123. using System;
  124. using System.Collections.Generic;
  125. using Prism.Interactivity.InteractionRequest;
  126.  
  127. public class OpenFileDialogInteractionContext : Confirmation
  128. {
  129. public IEnumerable<string> Filenames { get; set; }
  130. }
  131. }
  132.  
  133. namespace MyApp.Windows.Services
  134. {
  135. using System;
  136. using System.Windows;
  137. using System.Windows.Interactivity;
  138. using Prism.Interactivity.InteractionRequest;
  139.  
  140. public class PopupOpenFileDialogAction : TriggerAction<DependencyObject>
  141. {
  142. public static readonly DependencyProperty DialogServiceProperty = DependencyProperty.Register(
  143. nameof(DialogService), typeof(OpenFileDialogService), typeof(PopupOpenFileDialogAction), new PropertyMetadata());
  144.  
  145. public static readonly DependencyProperty OwnerProperty = DependencyProperty.Register(
  146. nameof(Owner), typeof(Window), typeof(PopupOpenFileDialogAction), new PropertyMetadata());
  147.  
  148. public OpenFileDialogService DialogService
  149. {
  150. get { return (OpenFileDialogService)this.GetValue(PopupOpenFileDialogAction.DialogServiceProperty); }
  151. set { this.SetValue(PopupOpenFileDialogAction.DialogServiceProperty, value); }
  152. }
  153.  
  154. public Window Owner
  155. {
  156. get { return (Window)this.GetValue(PopupOpenFileDialogAction.OwnerProperty); }
  157. set { this.SetValue(PopupOpenFileDialogAction.OwnerProperty, value); }
  158. }
  159.  
  160. protected override void Invoke(object parameter)
  161. {
  162. if (this.DialogService == null)
  163. {
  164. return;
  165. }
  166.  
  167. var interaction = (InteractionRequestedEventArgs)parameter;
  168. var context = (OpenFileDialogInteractionContext)interaction.Context;
  169.  
  170. context.Filenames = this.Owner !=null? this.DialogService.ShowDialog(this.Owner) : this.DialogService.ShowDialog();
  171. context.Confirmed = context.Filenames != null;
  172.  
  173. interaction.Callback?.Invoke();
  174. }
  175. }
  176. }
Add Comment
Please, Sign In to add comment