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?
Answer: 1
it's not a best practices for mvvm but that one way u can do that......
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); }
by : Sanjay Patelhttp://stackoverflow.com/users/1887299Answer: 2
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.
by : Geertenhttp://stackoverflow.com/users/907414Answer: 3
You should move your creation code to View, and ViewModel should just notify view when it should be called.
by : Lonli-Loklihttp://stackoverflow.com/users/462669Answer: 4
Do not use solution provided by Sanjay Patel, you are violating whole MVVM purpose and you will not be able to test that code.
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() } }
by : nihiquehttp://stackoverflow.com/users/75845Answer: 5
before going to ViewMode come up your codebehind and catch target control like that
Dont give up ViewModel in xaml instade of that use the event handler
This provides you a new Layer you can build your architecture like this MVVM => MV(C)VM
by : Hamit Yıldırımhttp://stackoverflow.com/users/914284
No comments:
Post a Comment
Send us your comment related to the topic mentioned on the blog