How can i access a control in mvvm model in viewmo

2019-01-29 23:47发布

I have a WPF Window, and in that window I have a grid.

I use M-V-VM model and I want to add a TextBox to the grid dynamically in code(in viewmodel)

How can I get access to the grid?

4条回答
聊天终结者
2楼-- · 2019-01-30 00:34

You should move your creation code to View, and ViewModel should just notify view when it should be called.

查看更多
劳资没心,怎么记你
3楼-- · 2019-01-30 00:40

If you are using Caliburn Micro, implement following step:

  1. Make the ViewModel inherited from interface IViewAware; you are going to implement two methods AttachView and GetView of this interface.

  2. Define a variable with type of View to get the reference to the View

  3. See detail below:

    private SomeViewClass v;
    public void AttachView(object view, object context = null)
    {
        v = view as BomView;
        if (ViewAttached != null)
             ViewAttached(this,
             new ViewAttachedEventArgs() { Context = context, View = view });
    }
    
    public object GetView(object context = null)
    {
        return v;
    }
    

Later on you can access a single element on the View through v such as v.txtName="John"; etc...

查看更多
老娘就宠你
4楼-- · 2019-01-30 00:48

Use Supervising Controller pattern.

Reading:

Example implementation for CaliburnMicro MVVM framework is shown here (will work same for all other frameworks - or you can do it by hand if you are doing MVVM by yourself):

http://drc.ideablade.com/devforce-2012/bin/view/Documentation/cocktail-tutorial-talk-to-view

Example:

1) Define interface IView in which ViewModel (VM) will talk to View with required method(s)

public interface IView 
{
    void AddTextBoxToGrid();
}

2) Inherit code behind View from your IView and implement IView.AddTextboxToGrid() method

public partial class View : IView 
{
    public void AddTextBoxToGrid() 
    {  
        // implement here your custom view logic using standard code behind; 
    }
}

3) Add property of type IView to your VM

public class ViewModel 
{
    public IView View { get; set; }
}

4) Set View property on VM to instance of View as IView e.g. in code behind DataContext.View = this as IView; or in Caliburn you can use IScreen.OnViewAttached override method)

public partial class View : IView 
{
    public View()
    {
        // access you VM by strategy of your framework or choice - this example is when you store your VM in View's DataContext
        (DataContext as ViewModel).View = this as IView;
    } 

    public void AddTextBoxToGrid() 
    {  
        // implement here your custom view logic using standard code behind; 
    }
}

5) In your VM call IView.AddTextboxToGrid()

public class ViewModel 
{
    public IView View { get; set; }

    public void AddTextBoxToGrid() 
    {
        if (View == null) return;
        View.AddTextBoxToGrid()
    }
}
查看更多
Summer. ? 凉城
5楼-- · 2019-01-30 00:51

You can also use the DataContext (which is the ViewModel) of the View in the code behind of the view, and add the textbox to the grid there. That would make more sense.

If you give the grid a name in your XAML file, you will be able to access the grid in the code behind immediately.

查看更多
登录 后发表回答