Reputation: 25
here is my helper class to use in different ViewModelClasses in my project;
public class MOpenFileDialog : ViewModelBase
{
#region Properties
private OpenFileDialog _openDialog;
public OpenFileDialog Dialog
{
get { return _openDialog; }
set
{
_openDialog = value;
RaisePropertyChanged(nameof(Dialog));
}
}
// some other properties
#endregion
#region Constructors
public MOpenFileDialog()
{
OpenFileCommand = new RelayCommand<object>(OpenFile);
Dialog = new OpenFileDialog()
{
FileName = "Documen",
Filter = "All Files|*.*",
};
}
#endregion
#region Methods
public void OpenFile(object param)
{
// somecode
}
// some other methods
#endregion
#region Commands
public ICommand OpenFileCommand { get; set; }
#endregion
}
and here in myClassVM i have an instant of MOpenFileDialog and i'm trying to add a new method to OpenFileCommand;
public class myClassVM : ViewModelBase
{
#region Properties
public MOpenFileDialog Opendial { get; set; }
#endregion
#region Constructor
public CollectNamesVM()
{
Opendial = new MOpenFileDialog() { };
Opendial.Dialog.Multiselect = true;
//Opendial.OpenFileCommand += CollectFilesFolder; // Wrong Way
}
#endregion
#region Methods
public void CollectFilesFolder()
{
// toDo
}
}
i want to add another method to be implemented when openFileCommand fired, i did a wrong trial just to explain what i want to do.
any help will be appreciated.
Upvotes: 0
Views: 535
Reputation: 825
Use delegate
in 5 step below:
Step 1. Create a delegate
. Example you want to pass an string[]
(string
array, data type
of FileNames
with your code: Opendial.Dialog.Multiselect = true
)
Step 2. Create a event
using the delegate
in step 1
Step 3. Raise the event
created in step 2 everywhen you want in MOpenFileDialog
Step 4. I cant know what do you want here, but Its same as this
Step 5. CollectFilesFolder
method 's params are same as the delegate 's params
// 1. Create a delegate. Example you want to pass an string array
public delegate void OpenFileCommand(string[] fileNames);
public class MOpenFileDialog : ViewModelBase
{
// 2. Create a event using the delegate in step 1
public event OpenFileCommand OnOpenFileCommand;
// 3. Raise the event created in step 2 everywhen you want
public void SomeMethod(string[] fileNames)
{
this.OnOpenFileCommand.Invoke(fileNames);
}
#region Properties
private OpenFileDialog _openDialog;
public OpenFileDialog Dialog
{
get { return _openDialog; }
set
{
_openDialog = value;
RaisePropertyChanged(nameof(Dialog));
}
}
// some other properties
#endregion
#region Constructors
public MOpenFileDialog()
{
OpenFileCommand = new RelayCommand<object>(OpenFile);
Dialog = new OpenFileDialog()
{
FileName = "Documen",
Filter = "All Files|*.*",
};
}
#endregion
#region Methods
public void OpenFile(object param)
{
// somecode
}
// some other methods
#endregion
#region Commands
public ICommand OpenFileCommand { get; set; }
#endregion
}
public class myClassVM : ViewModelBase
{
#region Properties
public MOpenFileDialog Opendial { get; set; }
#endregion
#region Constructor
public CollectNamesVM()
{
Opendial = new MOpenFileDialog() { };
Opendial.Dialog.Multiselect = true;
// 4. I cant know what do you want here, but Its same as this
Opendial.OnOpenFileCommand += CollectFilesFolder(Opendial.Dialog.FileNames); // Wrong Way
}
#endregion
#region Methods
// 5. CollectFilesFolder has params same as the delegate
public void CollectFilesFolder(string[] fileNames)
{
// toDo
}
}
Upvotes: 1