Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <Window x:Class="MyApp.Windows.Views.MainWindow"
- 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:services="clr-namespace:MyApp.Windows.Services"
- xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
- xmlns:prism="http://prismlibrary.com/"
- mc:Ignorable="d"
- x:Name="Window"
- prism:ViewModelLocator.AutoWireViewModel="True">
- <Window.Resources>
- <services:OpenFileDialogService x:Key="OpenFileDialogService" Title="Select File" Filter="Text File (*.txt)|*.txt|All Files (*.*)|*.*" DefaultFilterIndex="0" Multiselect="False" />
- </Window.Resources>
- <i:Interaction.Triggers>
- <prism:InteractionRequestTrigger SourceObject="{Binding SelectFileInteractionRequest, Mode=OneWay}">
- <services:PopupOpenFileDialogAction DialogService="{StaticResource OpenFileDialogService}" Owner="{Binding ElementName=Window}" />
- </prism:InteractionRequestTrigger>
- </i:Interaction.Triggers>
- <StackPanel Orientation="Vertical">
- <TextBox Text="{Binding Filename}"/>
- <Button Content="_Select File..." Command="{Binding SelectFileCommand}"/>
- </StackPanel>
- </Window>
- namespace MyApp.Windows.ViewModels
- {
- using System;
- using System.Linq;
- using System.Windows;
- using System.Windows.Input;
- using Prism.Mvvm;
- using Prism.Commands;
- using Prism.Interactivity.InteractionRequest;
- using MyApp.Windows.Services;
- public class MainWindowViewModel : BindableBase
- {
- private string filename;
- public MainWindowViewModel()
- {
- this.SelectFileCommand = new DelegateCommand(this.SelectFile);
- this.SelectFileInteractionRequest = new InteractionRequest<OpenFileDialogInteractionContext>();
- }
- public string Filename
- {
- get => this.filename;
- set => this.SetProperty(ref this.filename, value);
- }
- public ICommand SelectFileCommand { get; }
- public InteractionRequest<OpenFileDialogInteractionContext> SelectFileInteractionRequest { get; }
- private void SelectFile()
- {
- this.RaiseSelectFileInteractionRequest();
- }
- private void RaiseSelectFileInteractionRequest()
- {
- var context = new OpenFileDialogInteractionContext();
- this.SelectFileInteractionRequest.Raise(
- context,
- c => this.UpdateFilename(c));
- }
- private void UpdateFilename(OpenFileDialogInteractionContext context)
- {
- if (context.Confirmed)
- {
- this.Filename = context.Filenames.First();
- }
- }
- }
- }
- namespace MyApp.Windows.Services
- {
- using System;
- using System.Windows;
- using Microsoft.Win32;
- public class OpenFileDialogService
- {
- public string Filter { get; set; } = string.Empty;
- public string Title { get; set; } = string.Empty;
- public int DefaultFilterIndex { get; set; } = 0;
- public bool Multiselect { get; set; }
- public string[] ShowDialog(Window owner)
- {
- OpenFileDialog dialog = new OpenFileDialog()
- {
- Title = this.Title,
- Filter = this.Filter,
- FilterIndex = this.DefaultFilterIndex,
- Multiselect = this.Multiselect
- };
- var dialogResult = owner != null ? dialog.ShowDialog(owner) : dialog.ShowDialog();
- if (!dialogResult.HasValue || !dialogResult.Value)
- {
- return null;
- }
- return dialog.FileNames;
- }
- public string[] ShowDialog()
- {
- return this.ShowDialog(null);
- }
- }
- }
- namespace MyApp.Windows.Services
- {
- using System;
- using System.Collections.Generic;
- using Prism.Interactivity.InteractionRequest;
- public class OpenFileDialogInteractionContext : Confirmation
- {
- public IEnumerable<string> Filenames { get; set; }
- }
- }
- namespace MyApp.Windows.Services
- {
- using System;
- using System.Windows;
- using System.Windows.Interactivity;
- using Prism.Interactivity.InteractionRequest;
- public class PopupOpenFileDialogAction : TriggerAction<DependencyObject>
- {
- public static readonly DependencyProperty DialogServiceProperty = DependencyProperty.Register(
- nameof(DialogService), typeof(OpenFileDialogService), typeof(PopupOpenFileDialogAction), new PropertyMetadata());
- public static readonly DependencyProperty OwnerProperty = DependencyProperty.Register(
- nameof(Owner), typeof(Window), typeof(PopupOpenFileDialogAction), new PropertyMetadata());
- public OpenFileDialogService DialogService
- {
- get { return (OpenFileDialogService)this.GetValue(PopupOpenFileDialogAction.DialogServiceProperty); }
- set { this.SetValue(PopupOpenFileDialogAction.DialogServiceProperty, value); }
- }
- public Window Owner
- {
- get { return (Window)this.GetValue(PopupOpenFileDialogAction.OwnerProperty); }
- set { this.SetValue(PopupOpenFileDialogAction.OwnerProperty, value); }
- }
- protected override void Invoke(object parameter)
- {
- if (this.DialogService == null)
- {
- return;
- }
- var interaction = (InteractionRequestedEventArgs)parameter;
- var context = (OpenFileDialogInteractionContext)interaction.Context;
- context.Filenames = this.Owner !=null? this.DialogService.ShowDialog(this.Owner) : this.DialogService.ShowDialog();
- context.Confirmed = context.Filenames != null;
- interaction.Callback?.Invoke();
- }
- }
- }
Add Comment
Please, Sign In to add comment