Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPFC#4.0MVVM
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?
Posted 1-Nov-10 21:52pm
Edited 1-Nov-10 22:39pm
Dalek Dave426.2K
v2
Comments
Dalek Dave at 2-Nov-10 4:39am
   
Edited for Grammar, Syntax and Readability.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

As I understand MVVM, you shouldn't access view (or UI elements) directly from view model (or another layer below UI). If you need something like this, you should do it in view layer only.
  Permalink  
Comments
Dalek Dave at 2-Nov-10 4:39am
   
Good Answer.
Martin.Smith at 9-Nov-10 11:19am
   
Completely agree - ViewModel should not know anything about the view (or the controls it contains)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I write it
 Grid grid = new Grid();
            grid = App.Current.Windows[1].FindName("grid1") as Grid;
            if (grid != null)
            {
                RowDefinition rd = new RowDefinition();
                rd.Height = GridLength.Auto;
                grid.RowDefinitions.Add(rd);
                TextBox txt = new TextBox();
                txt.Name = "txtNewAdd";
                txt.Height = 23;
                txt.VerticalAlignment = VerticalAlignment.Top;
 
                //txtFname is in view Layer
                TextBox txtName = new TextBox();
                txtName= grid.FindName("txtFname") as TextBox;
                if (txtName!=null)
                {
                    txt.Background = txtName.Background ;
                    
                }
                Grid.SetColumn(txt, 1);
                Grid.SetRow(txt,(grid.RowDefinitions.Count-1));
                
                grid.Children.Add(txt);
            }
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

While probably not optimal given the MVVM best practices, you could have an ObservableCollection(Of UIElement) to which the Grid or ItemsControl is bound.
 
Your ViewModel or Model would create the new control and add it to the collection.
  Permalink  
Comments
Marcus Kramer at 7-Jan-13 12:04pm
   
Please check the date that a question was posted on. The chances that the OP is still looking for a solution from over two years ago is highly unlikely, and the question already has two solution posted. If the question is this old, please don't add new answers to it as it just bumps it back to the front of the queue which it shouldn't be doing. Cheers and welcome to CP.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 515
1 OriginalGriff 498
2 Abhinav S 305
3 sanket saxena 295
4 thatraja 235
0 Sergey Alexandrovich Kryukov 8,482
1 OriginalGriff 4,830
2 Peter Leow 3,794
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid