问题
There are lots of questions like this one, but still can't get what I really want, and all of them have something that differs of mine and that is: I have a UserControl:
- Built separately in a class library project called UCProject;
- The project UCProject has a View for the control, and its ViewModel;
- The UserControl binds some of its own controls and UIElements properties in the View to properties declared in the ViewModel in, of course, the UCProject;
- How can I show or expose or make the ViewModel Properties accessible to the page or the window of the project (that may be called for instance GlobalProject) that may host this UserControl;
I'am building this UserControl, I want to build events, properties to it ... and I want it to be used by others as a given assembly to them, so its code is not accessible, they only can consume it, I want to respect the MVVM pattern, and I don't have a clear idea how to realize that, should I write this properties and events in the CodeBehind of the UserControl View or should I put them in the ViewModel, and in that case how can I access them from outside, just like we daily use third-party controls
回答1:
If i assume it right, you want to expose properties of child UserControl's ViewModel to containing parent root element which might be window or other UserControl.
There are two approaches to it:
First, DataContext
DP is inheritable i.e. child controls inherit it from parent control unless set it explicitly to some other value. So, what you can do is have common ViewModel and set it as DataContext on parent UserControl and both have access to its properties
.
Second, in case you want separate ViewModels for parent and child UserControls. You can always access properties of child's ViewModel via DataContext
. Let me explain with an example:
<UserControl x:Name="ParentUserControl">
<StackPanel>
<local:ChildUserControl x:Name="Child"/>
<TextBlock Text="{Binding DataContext.PropertyName, ElementName=Child}"/>
</StackPanel>
</UserControl>
where PropertyName
is property in ViewModel of ChildUserControl.
Here as you can see TextBlock which lies in ParentUserControl is binding to property in ViewModel of child UserControl.
回答2:
You haven't specifically declared which parts of these projects you have control over so I'll assume you can changed all of them. Basically you need to use dependency properties in the User Control. First you bind the view to the ViewModel, when you add the User Control to the view it will effectively inherit the DataContext and you can bind the dependency properties you've created to the various parts of the ViewModel. In the User Control itself you bind the various FrameworkElements to the dependency properties in the User Control, not to the ViewModel itself.
Make sense?
回答3:
I had to implement those Properties in the CodeBehind using DependencyProperties that's the best way to do it
来源:https://stackoverflow.com/questions/20809901/expose-the-properties-of-a-usercontrol-which-is-built-in-mvvm