I'm developing a WPF C# app where I have a tree view control and if the user clicks on a node in the tree, a node-specific detail 'form' appears in a named Grid somewhere else in the form. If you click on a different node in the tree, the displayed detail form checks if the contents are saved, is dismissed, and a new detail form appears in it's place.
What I need is some starting advice. Can I still implement the forms as standalone xaml, then put some some of 'container' in the grid that I throw the form into as a child? Or just add the form as a grid child somehow. How do I programmatically load the form I want in the grid and communicate with it?
Thanks for any assistance!
Corey.
Use an event aggregator design pattern, see here for details:
http://martinfowler.com/eaaDev/EventAggregator.html
You can then have some other code which listens for the node change clicks via the event aggregator and response accordingly. This will decouple your code and make it more testable.
Am assuming you are using mvvm?
If not read up on it - will make it easier.
then you have your form with the treeview on it, bound to its itemsource on the view model.
Usually an items control like a treeview will have a selecteditem property on it.
Bind that to a property on your viewmodel which is of the type of objects contained in your treeview. Call this for example CurrentlySelectedItem.
Your details 'form' can be a control or whatever you want on the same form.
Now depending on how complete your object is - you have at least two options. If your object in the treeview has all the data you need already, then just bind the details to CurrentlySelectedItem.
Obviously it must implement INotifyPropertyChanged to tell the binding system to update the values.
If the object doesn't have enough info, then on the setter of CurrentlySelectedItem you can then fire a method to load the full object and then bind the details to that full object.
Alternativley, another popular approach, you could have the details form as a self contained control that subscribes to a message and when it recieves the message with the key of the treeview object, it loads the required info.
Related
I need to build a custom items control in WPF, where the user can drag/drop the items. Normally I would just maintain a list of view models and use a data template to define how the items should be displayed (in this case, a button). But I am concerned that this will make the drag/drop difficult as the ItemsSource objects will be view model objects , not the actual button.
My other potential approach is when an object is added to the ItemsSource, create a button in c# and add it manually, that way I can access the button directly to do drag/drop.
What would your advice be?
I have solved my issue. I found a way to get the control being render from the data template
I have dialog which consist of two user controls bind to their models.
I want to enable user to pick a data point on the chart control from the UserControl1.
I can get the data point on mouse down event of chart control and wan to fill the associated textbox with that value. I am unable to identify the best approach to pass that value back to the UserControl1. Also how will I identify which button has made the call to pick the data point. Note I have to do the same in an MVVM friendly manner.
Its not really about UserControl, more about ViewModels. I suggest your chart Model (which I guess should be a "ViewModel") could raise an event passing the data point as parameter.
To identify which button has made the call, you can memorise it in your UserControl Model (again, ViewModel).
I have a user control on a page and I'd like to load another custom user control within it's grid. However I want the user control type to be dynamic - e.g selecting "Calculator" from a drop down list would display my custom calculator control and selecting "Currency Converter" would load my currency converter. Can anybody help?
By the way I am trying to stick to MVVM.
Thanks in advance.
One way is to define multiple DataTemplates one for each child. As user selects the option set the corresponding ViewModel to a ContentControl's DataContext present in parent view and framework will pick the corresponding view for you.
John Papa has written a great blog post on exactly how to do this here.
The basic strategy is to instantiate a new instance of a class that derives from UserControl once selected from the drop down.
In other words, the SelectionChanged event of the drop down list could be handled and a new UserControl object created based on the selected item.
I searched this site and i found 2 Links
how to load wpf usercontrol in MVVM pattern
MVVM-Light: Load UserControl into Window
but still i cant find the answer to my problem and this link
MVVM-Light Locator Pattern and Reusable UserControl
i Didn't Understood.... so here is i am stating my problem which might help others struggling like me ......
I have a MainWindow which has 2 parts one has a TreeView(a
UserControl) and the other Displays different user controls(named DisplayPanel).... just like windows Explorer.
The Display Panel on the Right side will display different user controls on Clicking nodes of tree view.
and my TreeView is Itself a user Control.
How can i make this composite UI Work using MVVM. Also I am planning to use MVVM light Toolkit. Does this have something that can help...
An Example will be great
Thanks... :)
Edit
My TreeView in a UserControl I made a dependency property in the UserControl which catches the selected Item fo the tree view so that i can use this dependency property to populate the required view in the "MainView" ContentControl binding....as you advised me in the comments. Everything is till now
Problem is that i want to display data contained in the the selected item and i cannot set the DataContext of the UserControls(which will be displayed in right hand side) to the selected item as then i will not be able to use my view model for the respective usercontrol for commands and other operations
I tried to solve this too.... i used the Mediator (Messenger) in my TreeViewUserControl view model to send a Message to the Usercontrol's(the one that i need to display) view model . Message will be passed whenever the item is selected in the tree view. and message contains the selected node. I forgot to mention i set the datacontext of the UserControl to its view model so that i could display data
But using this approach the problem is that when the I click a type of node for the first time the data is not populated but if the same type of node is clicked again its populated. What’s happening is that UserControls object is availabe when the the tree item is clicked for the first time and Mediator sends the message. So Mediator is not able to pass the message to the userControl view model.....
I totally do not have ne idea to solve this further.... is my way if displaying user control right or I should do something that else....totally confused.....
You could try defining a DataTemplate for each type in the TreeView's ItemsSource and instead of having a specific UserControl on the right side, just bind to the TreeView's SelectedItem. Alternatively, you could use a DataTemplateSelector.
Edited for OP's Edit
Did you do this?
MainWindow has TreeView whose ItemsSource=Binding MainVM.Items.
MainWindow has ContentControl whose Content=Binding TreeView.SelectedItem.
Somewhere in project, have ResourceDictionary where each possible type in MainVM.Items has a DataTemplate defined?
Which ViewModel (MainVM or ItemVM) are you trying to use and why can't you use it?
I am working on Silverlight project.
I added a custom user control (Say, control1) which has a text box and button to a xaml page (Say, Page1).
Now what I want to do is when users clicks on the button, i want to pass the value in the textbox to Page1 and do something.
So basically, I am looking for a way to pass back a value from child to parent page in Silverlight.
Thank you.
You should look into the Model View ViewModel (MVVM) pattern. It works very well with WPF and Silverlight.
http://en.wikipedia.org/wiki/Model_View_ViewModel
http://karlshifflett.wordpress.com/mvvm/ (lots of good information and demos)
You can do this through binding. Bind the Text value of the TextBox to a string property in your ViewModel and use that property throughout the code.
All the controls within your user control are accessible within your main page. If possible, write the click event of the button within the main page and you'll be able to access any control's property. Hope that work for you.