Click here to Skip to main content
Sign Up to vote bad
good
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:52
Edited 1 Nov '10 - 22:39

Comments
Dalek Dave - 2 Nov '10 - 4:39
Edited for Grammar, Syntax and Readability.

3 solutions

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 - 2 Nov '10 - 4:39
Good Answer.
Martin.Smith - 9 Nov '10 - 11:19
Completely agree - ViewModel should not know anything about the view (or the controls it contains)
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  
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 - 7 Jan '13 - 12:04
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 564
1 Maciej Los 255
2 CPallini 245
3 Aarti Meswania 173
4 Mahesh Bailwal 171
0 Sergey Alexandrovich Kryukov 9,162
1 OriginalGriff 7,179
2 CPallini 3,913
3 Rohan Leuva 3,176
4 Maciej Los 2,588


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