MVVM access to another view element from viewModel

I started working with the MVVM template in a new project. Everything is in order, but I came to the next problem. The implementation is as follows: I have MainView, the main application window. In this window, I have a telerik RadGroupPanel, in which I place other types of applications as tabs. The rest of viewModels does not know about this RadGroupPanel, which is hosted in Mainview. How to correctly add these views to RadGroupPanel from commands in viewModels? Thank.

+2
source share
2 answers

Do you think you are entering your view in the ViewModel using an interface to maintain separation? I know this violates MVVM, but I have successfully used this in a number of WPF projects. I call it MiVVM or ModelModel to view the ViewModel of the model .

The template is simple. Your Usercontrol should have an interface, name it IView. Then in ViewModel you have a property with an installer of type IMyView, say

public IMyView InjectedView { set { _injectedView = value; } }

Then, a dependency property called This is created in the view.

public MyUserControl : IMyView
{
    public static readonly DependencyProperty ThisProperty = 
         DependencyProperty.Register("This", typeof(IMyView), typeof(MyUserControl)); 

    public MyUserControl() 
    {
       SetValue(ThisProperty, this);
    } 
    public IMyView This { get { return GetValue(ThisProperty); } set { /* do nothing */ } } 
}

Finally, in Xaml, you can enter a view directly in the ViewModel using the binding

<MyUserControl This="{Binding InjectedView, Mode=OneWayToSource}"/>

! , , . , (Viewmodel , IView ), . , . , ?

, . Attached Property MiVVM, .

+1

, ObservableCollection . ItemsSource RadGroupPanel ItemTemplateSelector ContentTemplateSelector RadGroupPanel, .

0

All Articles