Reputation: 109
I'm struggling with using SaveFileDialog in MVVM.
I'm using RelayCommand class and launch SaveAsFileCommand. Within the SaveAsFileCommand, using lambda expression I split two arguments to have:
Instance of RichTextBox control and targeted path (filePath)
Then I call DataIO.SaveAsFile(arguments[0], arguments[1]), using above arguments.
To create the SaveDialogBox in the View Layer I'm using 3 classes: DialogBox, FileDialogBox and SaveFileDialogBox
In the XAML I create the SaveDialogBox and try to call SaveAsFileCommand using MultiBinding to pass those two Command Parameters.
To show the SaveDialogBox I use the button which is bindded to SaveDialogBox
The problem is: At this place the compiler complains that it can't execute multibinding for my SaveDialogBox because of being non-DependencyObject and non-DependencyProperty.
How can I solve that problem and correctly save the file using that DialogBox
being in accordance with MVVM in my case???
XAML parts of code:
<Button Command="{Binding ElementName=SaveFileDB, Path=Show}" >
<Button.ToolTip>
<ToolTip Style="{StaticResource styleToolTip}" >
<TextBlock Text="Save" Style="{StaticResource styleTextBlockTP}" />
</ToolTip>
</Button.ToolTip>
<Image Source="Icon\Save.png"/>
</Button>
<local:SaveFileDialogBox x:Name="SaveFileDB" Caption="Save content to file..."
Filter="Text files (*.txt)|*.txt|All files(*.*)|*.*"
FilterIndex="0" DefaultExt="txt"
CommandFileOK="{Binding SaveAsFileCommand}" >
<local:SaveFileDialogBox.CommandParemeter>
<MultiBinding Converter="{StaticResource parametersConverter}">
<Binding ElementName="MainRichTextBox" />
<Binding ElementName="SaveFileDB" Path="Show" />
</MultiBinding>
</local:SaveFileDialogBox.CommandParemeter>
</local:SaveFileDialogBox>
SaveAsFileCommand:
private ICommand _SaveAsFileCommand;
public ICommand SaveAsFileCommand
{
get
{
if (_SaveAsFileCommand == null)
{
_SaveAsFileCommand = new RelayCommand(
argument =>
{
var arguments = (object[])argument;
DataIO.SaveAsFile(arguments[0], arguments[1]);
}
);
}
return _SaveAsFileCommand;
}
}
DataIO.SaveAsFile method:
public static void SaveAsFile(object argument0, object argument1)
{
System.Windows.Controls.RichTextBox richTB = argument0 as System.Windows.Controls.RichTextBox;
string path = (string)argument1;
using (FileStream fileStream = new FileStream(path, FileMode.Create))
{
TextRange textRange = new TextRange(richTB.Document.ContentStart, richTB.Document.ContentEnd);
textRange.Save(fileStream, DataFormats.Text);
}
}
RelayCommand class:
class RelayCommand : ICommand
{
private readonly Action<object> _Execute;
private readonly Func<object, bool> _CanExecute;
public RelayCommand(Action<object> execute, Func<object, bool> canExecute)
{
if (execute == null) throw new ArgumentNullException("execute");
_Execute = execute;
_CanExecute = canExecute;
}
public RelayCommand(Action<object> execute)
{
if (execute == null) throw new ArgumentNullException("execute");
_Execute = execute;
}
public bool CanExecute(object parameter)
{
return _CanExecute == null ? true : _CanExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add
{
if (_CanExecute != null) CommandManager.RequerySuggested += value;
}
remove
{
if (_CanExecute != null) CommandManager.RequerySuggested -= value;
}
}
public void Execute(object parameter)
{
_Execute(parameter);
}
}
DialogBox class:
public abstract class DialogBox : FrameworkElement, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string parameter)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(parameter));
}
protected Action<object> execute = null;
public string Caption { get; set; }
protected ICommand show;
public virtual ICommand Show
{
get
{
if (show == null)
show = new RelayCommand(execute);
return show;
}
}
}
FileDialogBox class:
public abstract class FileDialogBox : CommandDialogBox
{
public bool? FileDialogResult { get; protected set; }
public string FilePath { get; set; }
public string Filter { get; set; }
public int FilterIndex { get; set; }
public string DefaultExt { get; set; }
protected Microsoft.Win32.FileDialog fileDialog = null;
protected FileDialogBox()
{
execute =
o =>
{
var values = (object[])o;
RelayCommand relCom1 = (RelayCommand)values[1];
fileDialog.Title = Caption;
fileDialog.Filter = Filter;
fileDialog.FilterIndex = FilterIndex;
fileDialog.DefaultExt = DefaultExt;
string filePath = "";
if (FilePath != null) filePath = FilePath; else FilePath = "";
//if (o != null) filePath = (string)o;
//if (o != null) filePath = (string)values[1];
if (o != null) filePath = relCom1.ToString();
if (!String.IsNullOrWhiteSpace(filePath))
{
fileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(filePath);
fileDialog.FileName = System.IO.Path.GetFileName(filePath);
}
FileDialogResult = fileDialog.ShowDialog();
OnPropertyChanged("FileDialogResult");
if (FileDialogResult.HasValue && FileDialogResult != null)
{
FilePath = fileDialog.FileName;
OnPropertyChanged("FilePath");
ExecuteCommand(CommandFileOK, FilePath);
};
};
}
public static DependencyProperty CommandFileOKProperty =
DependencyProperty.Register("CommandFileOK", typeof(ICommand), typeof(FileDialogBox));
public ICommand CommandFileOK
{
get { return (ICommand)GetValue(CommandFileOKProperty); }
set { SetValue(CommandFileOKProperty, value); }
}
}
SaveFileDialogBox class:
public class SaveFileDialogBox : FileDialogBox
{
public SaveFileDialogBox()
{
fileDialog = new SaveFileDialog();
}
}
Upvotes: 1
Views: 2466
Reputation: 12276
The way I handle a requirement for user input in a dialog is to use a control which goes in the view but has no UI.
I split the command to be done into two pieces.
Essentially these are show the dialog and invoke a command when you finish.
The control shows a dialog which grabs data and then invokes a command you give it via binding.
Since this is control you can bind fine and it's in the visual tree so it can get a reference to the window.
Please see confirmationrequestor in this:
https://gallery.technet.microsoft.com/WPF-User-Notification-MVVM-98940828
That's intended for confirming the deletion of a record but the same principle can be extended to a file picker with a little modification.
From that, the command invoked once the user clicks and closes the dialogue can capture any variables you need. If you bind them:
private RelayCommand _confirmCommand;
public RelayCommand ConfirmCommand
{
get
{
return _confirmCommand
?? (_confirmCommand = new RelayCommand(
() =>
{
confirmer.Caption = "Please Confirm";
confirmer.Message = "Are you SURE you want to delete this record?";
confirmer.MsgBoxButton = MessageBoxButton.YesNo;
confirmer.MsgBoxImage = MessageBoxImage.Warning;
OkCommand = new RelayCommand(
() =>
{
// You would do some actual deletion or something here
UserNotificationMessage msg = new UserNotificationMessage { Message = "OK.\rDeleted it.\rYour data is consigned to oblivion.", SecondsToShow = 5 };
Messenger.Default.Send<UserNotificationMessage>(msg);
});
RaisePropertyChanged("OkCommand");
ShowConfirmation = true;
}));
}
}
From the confirmationrequestor, invoking that command:
public static readonly DependencyProperty ShowConfirmDialogProperty =
DependencyProperty.Register("ShowConfirmDialog",
typeof(bool?),
typeof(ConfirmationRequestor),
new FrameworkPropertyMetadata(null
, new PropertyChangedCallback(ConfirmDialogChanged)
)
{ BindsTwoWayByDefault = true }
);
private static void ConfirmDialogChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if ((bool?)e.NewValue != true)
{
return;
}
ConfirmationRequestor cr = (ConfirmationRequestor)d;
Window parent = Window.GetWindow(cr) as Window;
MessageBoxResult result = MessageBox.Show(parent, cr.Message, cr.Caption, cr.MsgBoxButton, cr.MsgBoxImage);
if (result == MessageBoxResult.OK || result == MessageBoxResult.Yes)
{
if (cr.Command != null)
{
cr.Command.Execute(cr.CommandParameter);
}
}
cr.SetValue(ShowConfirmDialogProperty, false);
}
Upvotes: 1