Get Contents of a UserControl inside a StackPanel - wpf

I have a StackPanel inside a WPF page, and I am loading the same UserControl inside it depending on the user requests. Now for each of these UserControls, I need to get the items which are inside it. The UserControl consists of a Calendar, and 3 comboboxes. How can I get the items?
So far I have the following:-
foreach (UserControl child in stk.Children)
{
}
and child has the contents required, however I cannot find out how to get the contents of every child to validate and store in the database. I tried child.FindControl("combobox1") but that did not work.
Thanks for your help and time

You can get hold of the Content . However I strongly suggest you don't do those of kind of things in the UI, and you shouldn't get the data from the UI. the UI should get the data from a ViewModel or a Model, and update them when needed(This is achieved through Binding). so if you need the data, your ViewModel should be the place to look at, not the UI.

I have found the following thread which was very helpful!
How can I find WPF controls by name or type?
It uses a helper to find all the controls in a WPF page.
Thanks for all your help guys

Related

Listening a event from other form in WPF

Do you know how event from "Add_Form" can be listened from "MainForm"? I code in WPF 2010(Visual Studio).
My program has two forms - "MainForm" and "Add_Form". "MainForm" has "Student_DataGrid" which has a register of students and button("btn_add_student") which invokes "Add_Form"(Form which has textblocks for input data of students). After clicking "btn_add_student" shows "Add_Form", further, I fill in data in textblocks and push the button "btn_add_student". New student is added in Data Base and "Add_form" is closed. Then "Student_DataGrid" on "MainForm" must be reloaded.
In other words I want to listen event on Add_Form from MainForm. I can do it in C#, but I can't do it in WPF. I'll be glad to any help.
Seems you haven't explored the strength of WPF i.e. Binding. These tutorials will get you going Binding Tutorial and one from msdn.
All you need to do in your case is bind your grid to an ObservableCollection and while adding a new student, add the object in this collection. Grid will automatically get refreshed.
If "Add_Form" and "MainForm" are binding to the same source, "Student_DataGrid" on "MainForm" needn't reloaded.
WPF can do it for u.
I found the way to solve it:).
I just made whole class of events:).
Here you are!

Dynamic Databinding in WPF

I am filling up a Stackpanel with Textboxes. Each textbox, Should display the Value Property of a Class i have written.
The number of Textboxes is always different so i can't create a databinding in the xaml file.
Are there tutorials, that show how can i archive my goal?
Edit: Thanks for the replies so far. After reading the articles i still not clear how to implement my Problem.
Here is what I'm trying to achieve:
I have an SQL Server DB and there a Table with an ID and a Value.
I now load all Values and IDs i want to display into my application. (number of loaded rows is always differnt).
I need to display a textbox for each row and display the value there and after the value is changed i write the value back to the database.
I don't know how I should query the data and than bind it to the textboxes.
I don't need an implementations, I am happy with every piece of advice I can get.
yes there are a number of tutorials out there. It sound like you are interested in datatemplating.
I would suggest looking Here on MSDN. A few tutorials on databinding in general may be useful DataBinding on MSDN
If your class, classValue, has a public property Value then just create a List and bind that to a ListBox with the item DisplayValuePath = Value. Repeater controls are used to bind to collections.

WPF - how to bind to source in different page?

Could anyone answer a really frustrating newbie question please?! I've been hunting for an answer for a few days & have found answers to similar questions posed, but nothing that exactly solves my issue.
I'm trying to bind a textbox to the currently selected item in a listview (which itself is bound to an Observablecollection of objects, not sure if this matters).
The listview is in RecentEntities.xaml & the textbox is in Relationship.xaml, both of which are positioned on the Main Window from within Maincontrol.xaml. So they're in the same namespace but they're in different pages so using ElementName doesn't work as this just seems to look within the current page.
The issue I'm having is when trying to define the source of the textbox binding in Relationship.xaml, how do I reference the selected listview item in RecentEntities.xaml? I wondered about using RelativeSource, but this seems to only let you navigate up the tree to an ancestor of the current control. Because of how MainControl is set up, I would need to travel up to the parent of the textbox, then to a sibling of the parent then down to a child of a child of it in order to get to the listview!
I've explored (possibly not in enough depth) other options like resources, data context, including header files and have read something about Merged Resource Dictionaries but to be honest the more possibilities I explore, the more confused I'm getting about what I need to be doing.
Is this really as complicated as it seems?! Any pointers or help would be brilliant, thanks for taking the time to answer :) I haven't posted on here before, so if you need any code snippets please let me know.
Can you create a ViewModel that both Views will use? Have the selected item in your listview bind to a property in the ViewModel and have the textbox bind to the same property. As long as both views reference the same instance of the viewmodel, it should work.
With two XAML files you will have to use the model or viewmodel (depending on your choice of architecture) for synchronization.
For the listview you can use the SelectedValue to bind to a property, use Mode=OneWayToSource for this binding since you just want to update the property, not change the selection in the listview itself.

MVVM - Closing a tab when a record is deleted

I'm not really sure how to do this in the best MVVM way...
Basically, my main app opens up a search window that shows all records in a TabPanel. Then if a record is double clicked a new tab is opened with that record. Now, I'm trying to keep things MVVM, but I can't for the life of me figure out how to close the gui tabitem when a person deletes the record (why keep it open if the record is gone).
The only way I can figure out how to do it now is to pass the instance of the TabItem as a parameter of the DeleteCommand, which to me seems like a big no-no, but I can't for the life of me figure out how to accomplish this.
If you were going to do this in the true MVVM sense, then double-clicking a record would, behind the scenes, add a record to a collection of records. That record collection is the datasource for the tabs in your control. Simply removing that item from the list (usually an ObservableCollection<T>) would result in the UI updating and the tab being removed.
Which approach are you currently using to show the tab?
Edit (in response to comment):
That is not "true" MVVM. It doesn't matter if the tabs can be more than just records. You should create View Models which abstract those details, then just put your view model instances (RecordViewModel, ReportViewModel, etc) in an observable collection and bind to that. Use datatemplates to render the correct views for each tab's content based on the type of view model the current tab is being bound to.
Josh Smith wrote an amazing article describing how MVVM works. The sample application does something very similar to what you want to do.
The application displays a TabControl which displays 1 or more workspaces. The workspace area displays two different kinds of items. The tab items are closable. Take a look, I'm sure this will solve your problem.
WPF Apps With The Model-View-ViewModel Design Pattern

WPF: Switch Template based on UserControl Data

Trying to implement what I thought was a simple concept. I have a user control (view) bound to a view model which provides a list of data. I have added toggle buttons to the usercontrol and would like to allow the user to use these toggle buttons to switch out which template is used to show the data. All of the templates used for the data work, and they are very different from one another so it's not just simple changes to a single template. I'd like to get this as much in XAML as possible.
Here's what I have now:
Where the data appears I have <UserControl Template="{StaticResource ListSwitchingControlTemplate}" />
In that control template I have all "sub templates" - really it's just all 3 representations with their visibility set to Collapsed. Then I use a data trigger on that control template to show the currently selected view. This works, but I noticed that all 3 representations get bound - they each act like they are active (and they are I guess).
I'd rather be able to truly switch the template at run time. I tried converting the containing user control to use a ContentTemplate for itself, but that just messes up all of the binding I have elsewhere. If only UserControls could use DataTriggers I'd be ok.
Any suggestions on how to cleanly go about getting this behavior. I have an idea that I'm just missing something simple.
Thanks,
Dave
you could do it via code?
http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-a-datatemplateselector ???
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/4fd42590-8375-46d0-b7bc-6c217df0f0ba/
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/dbbbb5d6-ab03-49a0-9e42-686fd41e0714
One way to do this would be to use a DataTemplateSelector.
Basically, you create a class that inherits from DataTemplateSelector and override its SelectTemplate virtual function. The return value from the function is the DataTemplate you want to use and in that function you have access to the object and its properties, which you can use to decide which template to select.
There is an example on MSDN here:
http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector.aspx

Resources