WPF FolderBrowserDialog using Model-View-ViewModel (MVVM) in c#
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have to get FolderBrowserDialog once I click the browse button,but I should achieve that by using MVVM.
Explanation:
Once I get the FolderBrowserDialog Box, I should able to select the folder in which I'm wishing to save my files.
Then after selecting the folder,it should show me the selected folderpath along with the foldername in the Textbox beside my browse button.
How can I achieve this....
c# wpf mvvm
|
show 4 more comments
I have to get FolderBrowserDialog once I click the browse button,but I should achieve that by using MVVM.
Explanation:
Once I get the FolderBrowserDialog Box, I should able to select the folder in which I'm wishing to save my files.
Then after selecting the folder,it should show me the selected folderpath along with the foldername in the Textbox beside my browse button.
How can I achieve this....
c# wpf mvvm
1
It is not a responsibility of vm to show filedialog. You should do this in code behind and then use vm from DataContext property to call a method in vm
– FCin
Jan 4 at 6:47
Write method in your code behind with OpenFileDialog and in the same method cast(YourVM)DataContext
and call method in VM from it. That's it.
– FCin
Jan 4 at 7:18
1
You might want to check my article and code on MVVM Dialog Boxes. Going from memory, I don't think I specifically added the folder dialog chooser, but if not it'd only take a few lines to add if you base it off the one for OpenFileDialog.
– Mark Feldman
Jan 4 at 8:25
1
@MarkFeldman It's really a pity that people searching SO before searching the web in order to find such articles. Although SO also helped me when beginning, I think if I had a company, I would block the URL. In production I have witnessed so much copy&paste of bad code that caused a lot of time consuming trouble. All because of the lack of some basic understanding. I've worked with developers that can't understand their own code and thus were not able to fix it. As an excuse they said it was copied from SO. I will recommend your article any time this topic comes up again. And it will for sure.
– BionicCode
Jan 4 at 10:26
1
@Bionic it's a problem, to be sure. I've run a software company in the past and my two rules for cut-n-paste (be it SO or anywhere else) were 1) give every line at least a rough look-over to make sure it's doing what you think it's doing, and 2) add the URL you got it from to the top so that everyone at least knows where it came from. If you're also doing code-vetting then that should catch most problems, without the time/money hit of forcing complete re-writes for everything.
– Mark Feldman
Jan 4 at 10:41
|
show 4 more comments
I have to get FolderBrowserDialog once I click the browse button,but I should achieve that by using MVVM.
Explanation:
Once I get the FolderBrowserDialog Box, I should able to select the folder in which I'm wishing to save my files.
Then after selecting the folder,it should show me the selected folderpath along with the foldername in the Textbox beside my browse button.
How can I achieve this....
c# wpf mvvm
I have to get FolderBrowserDialog once I click the browse button,but I should achieve that by using MVVM.
Explanation:
Once I get the FolderBrowserDialog Box, I should able to select the folder in which I'm wishing to save my files.
Then after selecting the folder,it should show me the selected folderpath along with the foldername in the Textbox beside my browse button.
How can I achieve this....
c# wpf mvvm
c# wpf mvvm
edited Jan 8 at 9:38
GJPD
asked Jan 4 at 6:21
GJPDGJPD
196
196
1
It is not a responsibility of vm to show filedialog. You should do this in code behind and then use vm from DataContext property to call a method in vm
– FCin
Jan 4 at 6:47
Write method in your code behind with OpenFileDialog and in the same method cast(YourVM)DataContext
and call method in VM from it. That's it.
– FCin
Jan 4 at 7:18
1
You might want to check my article and code on MVVM Dialog Boxes. Going from memory, I don't think I specifically added the folder dialog chooser, but if not it'd only take a few lines to add if you base it off the one for OpenFileDialog.
– Mark Feldman
Jan 4 at 8:25
1
@MarkFeldman It's really a pity that people searching SO before searching the web in order to find such articles. Although SO also helped me when beginning, I think if I had a company, I would block the URL. In production I have witnessed so much copy&paste of bad code that caused a lot of time consuming trouble. All because of the lack of some basic understanding. I've worked with developers that can't understand their own code and thus were not able to fix it. As an excuse they said it was copied from SO. I will recommend your article any time this topic comes up again. And it will for sure.
– BionicCode
Jan 4 at 10:26
1
@Bionic it's a problem, to be sure. I've run a software company in the past and my two rules for cut-n-paste (be it SO or anywhere else) were 1) give every line at least a rough look-over to make sure it's doing what you think it's doing, and 2) add the URL you got it from to the top so that everyone at least knows where it came from. If you're also doing code-vetting then that should catch most problems, without the time/money hit of forcing complete re-writes for everything.
– Mark Feldman
Jan 4 at 10:41
|
show 4 more comments
1
It is not a responsibility of vm to show filedialog. You should do this in code behind and then use vm from DataContext property to call a method in vm
– FCin
Jan 4 at 6:47
Write method in your code behind with OpenFileDialog and in the same method cast(YourVM)DataContext
and call method in VM from it. That's it.
– FCin
Jan 4 at 7:18
1
You might want to check my article and code on MVVM Dialog Boxes. Going from memory, I don't think I specifically added the folder dialog chooser, but if not it'd only take a few lines to add if you base it off the one for OpenFileDialog.
– Mark Feldman
Jan 4 at 8:25
1
@MarkFeldman It's really a pity that people searching SO before searching the web in order to find such articles. Although SO also helped me when beginning, I think if I had a company, I would block the URL. In production I have witnessed so much copy&paste of bad code that caused a lot of time consuming trouble. All because of the lack of some basic understanding. I've worked with developers that can't understand their own code and thus were not able to fix it. As an excuse they said it was copied from SO. I will recommend your article any time this topic comes up again. And it will for sure.
– BionicCode
Jan 4 at 10:26
1
@Bionic it's a problem, to be sure. I've run a software company in the past and my two rules for cut-n-paste (be it SO or anywhere else) were 1) give every line at least a rough look-over to make sure it's doing what you think it's doing, and 2) add the URL you got it from to the top so that everyone at least knows where it came from. If you're also doing code-vetting then that should catch most problems, without the time/money hit of forcing complete re-writes for everything.
– Mark Feldman
Jan 4 at 10:41
1
1
It is not a responsibility of vm to show filedialog. You should do this in code behind and then use vm from DataContext property to call a method in vm
– FCin
Jan 4 at 6:47
It is not a responsibility of vm to show filedialog. You should do this in code behind and then use vm from DataContext property to call a method in vm
– FCin
Jan 4 at 6:47
Write method in your code behind with OpenFileDialog and in the same method cast
(YourVM)DataContext
and call method in VM from it. That's it.– FCin
Jan 4 at 7:18
Write method in your code behind with OpenFileDialog and in the same method cast
(YourVM)DataContext
and call method in VM from it. That's it.– FCin
Jan 4 at 7:18
1
1
You might want to check my article and code on MVVM Dialog Boxes. Going from memory, I don't think I specifically added the folder dialog chooser, but if not it'd only take a few lines to add if you base it off the one for OpenFileDialog.
– Mark Feldman
Jan 4 at 8:25
You might want to check my article and code on MVVM Dialog Boxes. Going from memory, I don't think I specifically added the folder dialog chooser, but if not it'd only take a few lines to add if you base it off the one for OpenFileDialog.
– Mark Feldman
Jan 4 at 8:25
1
1
@MarkFeldman It's really a pity that people searching SO before searching the web in order to find such articles. Although SO also helped me when beginning, I think if I had a company, I would block the URL. In production I have witnessed so much copy&paste of bad code that caused a lot of time consuming trouble. All because of the lack of some basic understanding. I've worked with developers that can't understand their own code and thus were not able to fix it. As an excuse they said it was copied from SO. I will recommend your article any time this topic comes up again. And it will for sure.
– BionicCode
Jan 4 at 10:26
@MarkFeldman It's really a pity that people searching SO before searching the web in order to find such articles. Although SO also helped me when beginning, I think if I had a company, I would block the URL. In production I have witnessed so much copy&paste of bad code that caused a lot of time consuming trouble. All because of the lack of some basic understanding. I've worked with developers that can't understand their own code and thus were not able to fix it. As an excuse they said it was copied from SO. I will recommend your article any time this topic comes up again. And it will for sure.
– BionicCode
Jan 4 at 10:26
1
1
@Bionic it's a problem, to be sure. I've run a software company in the past and my two rules for cut-n-paste (be it SO or anywhere else) were 1) give every line at least a rough look-over to make sure it's doing what you think it's doing, and 2) add the URL you got it from to the top so that everyone at least knows where it came from. If you're also doing code-vetting then that should catch most problems, without the time/money hit of forcing complete re-writes for everything.
– Mark Feldman
Jan 4 at 10:41
@Bionic it's a problem, to be sure. I've run a software company in the past and my two rules for cut-n-paste (be it SO or anywhere else) were 1) give every line at least a rough look-over to make sure it's doing what you think it's doing, and 2) add the URL you got it from to the top so that everyone at least knows where it came from. If you're also doing code-vetting then that should catch most problems, without the time/money hit of forcing complete re-writes for everything.
– Mark Feldman
Jan 4 at 10:41
|
show 4 more comments
1 Answer
1
active
oldest
votes
You need to learn about binding.
In this simple example i added a button that is bind to a Command - that replaces the code behind event.I used also in a Nuget that extant the ICommand and implement it ( along with a lot of other functionality ) - The name of the Nuget is Prism6.MEF.
Here is the example :
Xaml:
<Grid>
<StackPanel>
<TextBlock Text="{Binding BindableTextProperty}" />
<Button Content ="Do Action" Command="{Binding DoAction}" Height="50"/>
</StackPanel>
</Grid>
Code Behind :
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowVM();
}
}
View Model :
class MainWindowVM : BindableBase
{
private string m_bindableTextProperty;
public MainWindowVM()
{
DoAction = new DelegateCommand(ExecuteDoAction,CanExecuteDoAction);
}
public string BindableTextProperty
{
get { return m_bindableTextProperty; }
set { SetProperty(ref m_bindableTextProperty , value); }
}
public DelegateCommand DoAction
{
get;
private set;
}
private bool CanExecuteDoAction()
{
return true;
}
private void ExecuteDoAction()
{
// Do something
// You could enter the Folder selection code here
BindableTextProperty = "Done";
}
}
As i explained at the begin with to understand why it works you have to understand the Binding in WPF and especially the INotifyPropertyChange - for the Data on the TextBlock
Hope it helped :)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54033937%2fwpf-folderbrowserdialog-using-model-view-viewmodel-mvvm-in-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to learn about binding.
In this simple example i added a button that is bind to a Command - that replaces the code behind event.I used also in a Nuget that extant the ICommand and implement it ( along with a lot of other functionality ) - The name of the Nuget is Prism6.MEF.
Here is the example :
Xaml:
<Grid>
<StackPanel>
<TextBlock Text="{Binding BindableTextProperty}" />
<Button Content ="Do Action" Command="{Binding DoAction}" Height="50"/>
</StackPanel>
</Grid>
Code Behind :
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowVM();
}
}
View Model :
class MainWindowVM : BindableBase
{
private string m_bindableTextProperty;
public MainWindowVM()
{
DoAction = new DelegateCommand(ExecuteDoAction,CanExecuteDoAction);
}
public string BindableTextProperty
{
get { return m_bindableTextProperty; }
set { SetProperty(ref m_bindableTextProperty , value); }
}
public DelegateCommand DoAction
{
get;
private set;
}
private bool CanExecuteDoAction()
{
return true;
}
private void ExecuteDoAction()
{
// Do something
// You could enter the Folder selection code here
BindableTextProperty = "Done";
}
}
As i explained at the begin with to understand why it works you have to understand the Binding in WPF and especially the INotifyPropertyChange - for the Data on the TextBlock
Hope it helped :)
add a comment |
You need to learn about binding.
In this simple example i added a button that is bind to a Command - that replaces the code behind event.I used also in a Nuget that extant the ICommand and implement it ( along with a lot of other functionality ) - The name of the Nuget is Prism6.MEF.
Here is the example :
Xaml:
<Grid>
<StackPanel>
<TextBlock Text="{Binding BindableTextProperty}" />
<Button Content ="Do Action" Command="{Binding DoAction}" Height="50"/>
</StackPanel>
</Grid>
Code Behind :
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowVM();
}
}
View Model :
class MainWindowVM : BindableBase
{
private string m_bindableTextProperty;
public MainWindowVM()
{
DoAction = new DelegateCommand(ExecuteDoAction,CanExecuteDoAction);
}
public string BindableTextProperty
{
get { return m_bindableTextProperty; }
set { SetProperty(ref m_bindableTextProperty , value); }
}
public DelegateCommand DoAction
{
get;
private set;
}
private bool CanExecuteDoAction()
{
return true;
}
private void ExecuteDoAction()
{
// Do something
// You could enter the Folder selection code here
BindableTextProperty = "Done";
}
}
As i explained at the begin with to understand why it works you have to understand the Binding in WPF and especially the INotifyPropertyChange - for the Data on the TextBlock
Hope it helped :)
add a comment |
You need to learn about binding.
In this simple example i added a button that is bind to a Command - that replaces the code behind event.I used also in a Nuget that extant the ICommand and implement it ( along with a lot of other functionality ) - The name of the Nuget is Prism6.MEF.
Here is the example :
Xaml:
<Grid>
<StackPanel>
<TextBlock Text="{Binding BindableTextProperty}" />
<Button Content ="Do Action" Command="{Binding DoAction}" Height="50"/>
</StackPanel>
</Grid>
Code Behind :
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowVM();
}
}
View Model :
class MainWindowVM : BindableBase
{
private string m_bindableTextProperty;
public MainWindowVM()
{
DoAction = new DelegateCommand(ExecuteDoAction,CanExecuteDoAction);
}
public string BindableTextProperty
{
get { return m_bindableTextProperty; }
set { SetProperty(ref m_bindableTextProperty , value); }
}
public DelegateCommand DoAction
{
get;
private set;
}
private bool CanExecuteDoAction()
{
return true;
}
private void ExecuteDoAction()
{
// Do something
// You could enter the Folder selection code here
BindableTextProperty = "Done";
}
}
As i explained at the begin with to understand why it works you have to understand the Binding in WPF and especially the INotifyPropertyChange - for the Data on the TextBlock
Hope it helped :)
You need to learn about binding.
In this simple example i added a button that is bind to a Command - that replaces the code behind event.I used also in a Nuget that extant the ICommand and implement it ( along with a lot of other functionality ) - The name of the Nuget is Prism6.MEF.
Here is the example :
Xaml:
<Grid>
<StackPanel>
<TextBlock Text="{Binding BindableTextProperty}" />
<Button Content ="Do Action" Command="{Binding DoAction}" Height="50"/>
</StackPanel>
</Grid>
Code Behind :
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowVM();
}
}
View Model :
class MainWindowVM : BindableBase
{
private string m_bindableTextProperty;
public MainWindowVM()
{
DoAction = new DelegateCommand(ExecuteDoAction,CanExecuteDoAction);
}
public string BindableTextProperty
{
get { return m_bindableTextProperty; }
set { SetProperty(ref m_bindableTextProperty , value); }
}
public DelegateCommand DoAction
{
get;
private set;
}
private bool CanExecuteDoAction()
{
return true;
}
private void ExecuteDoAction()
{
// Do something
// You could enter the Folder selection code here
BindableTextProperty = "Done";
}
}
As i explained at the begin with to understand why it works you have to understand the Binding in WPF and especially the INotifyPropertyChange - for the Data on the TextBlock
Hope it helped :)
answered Jan 4 at 8:37
AsafAsaf
452
452
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54033937%2fwpf-folderbrowserdialog-using-model-view-viewmodel-mvvm-in-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
It is not a responsibility of vm to show filedialog. You should do this in code behind and then use vm from DataContext property to call a method in vm
– FCin
Jan 4 at 6:47
Write method in your code behind with OpenFileDialog and in the same method cast
(YourVM)DataContext
and call method in VM from it. That's it.– FCin
Jan 4 at 7:18
1
You might want to check my article and code on MVVM Dialog Boxes. Going from memory, I don't think I specifically added the folder dialog chooser, but if not it'd only take a few lines to add if you base it off the one for OpenFileDialog.
– Mark Feldman
Jan 4 at 8:25
1
@MarkFeldman It's really a pity that people searching SO before searching the web in order to find such articles. Although SO also helped me when beginning, I think if I had a company, I would block the URL. In production I have witnessed so much copy&paste of bad code that caused a lot of time consuming trouble. All because of the lack of some basic understanding. I've worked with developers that can't understand their own code and thus were not able to fix it. As an excuse they said it was copied from SO. I will recommend your article any time this topic comes up again. And it will for sure.
– BionicCode
Jan 4 at 10:26
1
@Bionic it's a problem, to be sure. I've run a software company in the past and my two rules for cut-n-paste (be it SO or anywhere else) were 1) give every line at least a rough look-over to make sure it's doing what you think it's doing, and 2) add the URL you got it from to the top so that everyone at least knows where it came from. If you're also doing code-vetting then that should catch most problems, without the time/money hit of forcing complete re-writes for everything.
– Mark Feldman
Jan 4 at 10:41