Usercontrol viewmodel wpf mvvm
I have a user control called AddUserControl which is used in MainWindow as a edit screen and there is add button in ribbon control, so the same AddUserControl should open. so i have created a AddWindow and plugged that usercontrol in AddWindow.
so should i make a viewmodel for AddUserControl cause same code is being repeated in both MainScreen and AddWindow? But usercontrol should not have its viewmodel. It will be really helpful if anyone will explain it?
c# wpf mvvm
add a comment |
I have a user control called AddUserControl which is used in MainWindow as a edit screen and there is add button in ribbon control, so the same AddUserControl should open. so i have created a AddWindow and plugged that usercontrol in AddWindow.
so should i make a viewmodel for AddUserControl cause same code is being repeated in both MainScreen and AddWindow? But usercontrol should not have its viewmodel. It will be really helpful if anyone will explain it?
c# wpf mvvm
1
If the UserControl is supposed to be independent of a specific view model, it has to expose dependency properties that can be bound to properties of any view model object in the current DataContext, like<local:YourUserControl UcProperty="{Binding VmProperty}"/>. If you put such a UserControl in a DataTemplate for a certain view model type (by setting the DataTemplate'sDataTypeproperty), it will be created automatically when you assign an instance of the view model type to theContentproperty of a ContentControl.
– Clemens
Nov 20 '18 at 12:00
The view model of theMainWindowmay have aUserproperty which returns anAddUserViewModelthat theAddUserControlbinds to. And you can set theDataContextof theAddWindowto another instance ofAddUserViewModel. Then theUserControlwill be reusable in both windows. The other option is to add dependency properties to theUserControland bind these individually.
– mm8
Nov 20 '18 at 12:16
In my AddUserControl, i have some methods like upload files and perform some action like edit and save. There is like more than 10+ properties, so making dependency properties is a difficult task i guess. still can you please explain further or maybe different approach?
– Ravi Solanki
Nov 20 '18 at 12:50
1
Can you post a visual like a UML or the code (View, any code behind, and ViewModel)?
– Michael Puckett II
Nov 20 '18 at 13:14
add a comment |
I have a user control called AddUserControl which is used in MainWindow as a edit screen and there is add button in ribbon control, so the same AddUserControl should open. so i have created a AddWindow and plugged that usercontrol in AddWindow.
so should i make a viewmodel for AddUserControl cause same code is being repeated in both MainScreen and AddWindow? But usercontrol should not have its viewmodel. It will be really helpful if anyone will explain it?
c# wpf mvvm
I have a user control called AddUserControl which is used in MainWindow as a edit screen and there is add button in ribbon control, so the same AddUserControl should open. so i have created a AddWindow and plugged that usercontrol in AddWindow.
so should i make a viewmodel for AddUserControl cause same code is being repeated in both MainScreen and AddWindow? But usercontrol should not have its viewmodel. It will be really helpful if anyone will explain it?
c# wpf mvvm
c# wpf mvvm
asked Nov 20 '18 at 11:36
Ravi Solanki
5916
5916
1
If the UserControl is supposed to be independent of a specific view model, it has to expose dependency properties that can be bound to properties of any view model object in the current DataContext, like<local:YourUserControl UcProperty="{Binding VmProperty}"/>. If you put such a UserControl in a DataTemplate for a certain view model type (by setting the DataTemplate'sDataTypeproperty), it will be created automatically when you assign an instance of the view model type to theContentproperty of a ContentControl.
– Clemens
Nov 20 '18 at 12:00
The view model of theMainWindowmay have aUserproperty which returns anAddUserViewModelthat theAddUserControlbinds to. And you can set theDataContextof theAddWindowto another instance ofAddUserViewModel. Then theUserControlwill be reusable in both windows. The other option is to add dependency properties to theUserControland bind these individually.
– mm8
Nov 20 '18 at 12:16
In my AddUserControl, i have some methods like upload files and perform some action like edit and save. There is like more than 10+ properties, so making dependency properties is a difficult task i guess. still can you please explain further or maybe different approach?
– Ravi Solanki
Nov 20 '18 at 12:50
1
Can you post a visual like a UML or the code (View, any code behind, and ViewModel)?
– Michael Puckett II
Nov 20 '18 at 13:14
add a comment |
1
If the UserControl is supposed to be independent of a specific view model, it has to expose dependency properties that can be bound to properties of any view model object in the current DataContext, like<local:YourUserControl UcProperty="{Binding VmProperty}"/>. If you put such a UserControl in a DataTemplate for a certain view model type (by setting the DataTemplate'sDataTypeproperty), it will be created automatically when you assign an instance of the view model type to theContentproperty of a ContentControl.
– Clemens
Nov 20 '18 at 12:00
The view model of theMainWindowmay have aUserproperty which returns anAddUserViewModelthat theAddUserControlbinds to. And you can set theDataContextof theAddWindowto another instance ofAddUserViewModel. Then theUserControlwill be reusable in both windows. The other option is to add dependency properties to theUserControland bind these individually.
– mm8
Nov 20 '18 at 12:16
In my AddUserControl, i have some methods like upload files and perform some action like edit and save. There is like more than 10+ properties, so making dependency properties is a difficult task i guess. still can you please explain further or maybe different approach?
– Ravi Solanki
Nov 20 '18 at 12:50
1
Can you post a visual like a UML or the code (View, any code behind, and ViewModel)?
– Michael Puckett II
Nov 20 '18 at 13:14
1
1
If the UserControl is supposed to be independent of a specific view model, it has to expose dependency properties that can be bound to properties of any view model object in the current DataContext, like
<local:YourUserControl UcProperty="{Binding VmProperty}"/>. If you put such a UserControl in a DataTemplate for a certain view model type (by setting the DataTemplate's DataType property), it will be created automatically when you assign an instance of the view model type to the Content property of a ContentControl.– Clemens
Nov 20 '18 at 12:00
If the UserControl is supposed to be independent of a specific view model, it has to expose dependency properties that can be bound to properties of any view model object in the current DataContext, like
<local:YourUserControl UcProperty="{Binding VmProperty}"/>. If you put such a UserControl in a DataTemplate for a certain view model type (by setting the DataTemplate's DataType property), it will be created automatically when you assign an instance of the view model type to the Content property of a ContentControl.– Clemens
Nov 20 '18 at 12:00
The view model of the
MainWindow may have a User property which returns an AddUserViewModel that the AddUserControl binds to. And you can set the DataContext of the AddWindow to another instance of AddUserViewModel. Then the UserControl will be reusable in both windows. The other option is to add dependency properties to the UserControl and bind these individually.– mm8
Nov 20 '18 at 12:16
The view model of the
MainWindow may have a User property which returns an AddUserViewModel that the AddUserControl binds to. And you can set the DataContext of the AddWindow to another instance of AddUserViewModel. Then the UserControl will be reusable in both windows. The other option is to add dependency properties to the UserControl and bind these individually.– mm8
Nov 20 '18 at 12:16
In my AddUserControl, i have some methods like upload files and perform some action like edit and save. There is like more than 10+ properties, so making dependency properties is a difficult task i guess. still can you please explain further or maybe different approach?
– Ravi Solanki
Nov 20 '18 at 12:50
In my AddUserControl, i have some methods like upload files and perform some action like edit and save. There is like more than 10+ properties, so making dependency properties is a difficult task i guess. still can you please explain further or maybe different approach?
– Ravi Solanki
Nov 20 '18 at 12:50
1
1
Can you post a visual like a UML or the code (View, any code behind, and ViewModel)?
– Michael Puckett II
Nov 20 '18 at 13:14
Can you post a visual like a UML or the code (View, any code behind, and ViewModel)?
– Michael Puckett II
Nov 20 '18 at 13:14
add a comment |
0
active
oldest
votes
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%2f53392165%2fusercontrol-viewmodel-wpf-mvvm%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53392165%2fusercontrol-viewmodel-wpf-mvvm%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
If the UserControl is supposed to be independent of a specific view model, it has to expose dependency properties that can be bound to properties of any view model object in the current DataContext, like
<local:YourUserControl UcProperty="{Binding VmProperty}"/>. If you put such a UserControl in a DataTemplate for a certain view model type (by setting the DataTemplate'sDataTypeproperty), it will be created automatically when you assign an instance of the view model type to theContentproperty of a ContentControl.– Clemens
Nov 20 '18 at 12:00
The view model of the
MainWindowmay have aUserproperty which returns anAddUserViewModelthat theAddUserControlbinds to. And you can set theDataContextof theAddWindowto another instance ofAddUserViewModel. Then theUserControlwill be reusable in both windows. The other option is to add dependency properties to theUserControland bind these individually.– mm8
Nov 20 '18 at 12:16
In my AddUserControl, i have some methods like upload files and perform some action like edit and save. There is like more than 10+ properties, so making dependency properties is a difficult task i guess. still can you please explain further or maybe different approach?
– Ravi Solanki
Nov 20 '18 at 12:50
1
Can you post a visual like a UML or the code (View, any code behind, and ViewModel)?
– Michael Puckett II
Nov 20 '18 at 13:14