Usercontrol viewmodel wpf mvvm












0














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?










share|improve this question


















  • 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












  • 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






  • 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
















0














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?










share|improve this question


















  • 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












  • 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






  • 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














0












0








0







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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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'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










  • 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




    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










  • 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












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
});


}
});














draft saved

draft discarded


















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
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Paul Cézanne

UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

Angular material date-picker (MatDatepicker) auto completes the date on focus out