Reputation: 861
I am just learning WPF so bear with me.
I have a viewmodel with an observable collection of (very simplified here for brevity) Employee objects that looks something like this:
public class Employee
{
public string Name { get; set; }
public string Title { get; set; }
}
In my C# WPF application, I am displaying information from this observable collection in a tab control in my view via simple data binding. Easy stuff so far. My requirement is that the user can dynamically add a new tab that displays only the information from the Employee that they are interested in and that new view of the Employee data will be saved and re-loaded each time the application is run.
So, let's say my user wants a new tab added that only displays those employees that are Software Engineers. I need to give them a method to choose their filter criteria and then add a new tab to the tab control that shows the employees that match the selected filter criteria. That custom view of the employee information in the newly created tab should then be saved and recreated each time the application is run.
I don't need full code examples, although that would really be helpful. What I would like is some suggestion for an approach so I can go google for information. I am just not sure which techniques are best to solve this problem. I am thinking dynamically created datatemplates or a custom CollectionViewSource, but then I need that information saved and reloaded so do I save the dynamically created datatemplate and use that when the application is loaded?
Upvotes: 0
Views: 231
Reputation: 1820
From what I understand, your tabs would each have a filter and an observable collection loaded based on this filter.
If this is the case, one possible solution would be to define a view model to represent the tab (EmployeesViewModel). This view model will have properties for the filter and observable collection plus any other methods to save / modify the observable collection.
public class EmployeesViewModel {
public Filter Filter { get; set;}
public ObservableCollection<Employee> Employees { get; set;}
public Employee SelectedEmployee { get; set;}
public RoutedCommand SaveSelectedEmployee { get; set;}
…
}
Once you have this, make the class serializable (by implementing ISerializable). You should only serialize the filter conditions, not the content of the collection.
When you shut down your application or when you teardown the tab viewer module, iterate through the tab collection, get the associated view model and serialize each of them. And when you start your application (or tab viewer module) recreate each tab using deserialized view model
Upvotes: 1
Reputation: 11439
What you need to do is create a TabControl template (http://msdn.microsoft.com/en-us/library/ms754137(v=vs.85).aspx), and then on the 'rightmost' tab have a button that let's you add new tabs, a la a web browser.
If you're still new at templating, I would reccomend practicing on templating a button, and then a textbox, then a expander until you get good.
Templating is central to how WPF works, so take the time to practice and get comfortable with it.
For saving and loading information on startup and so forth, I would look up 'Serialization', which is basically the process of taking your C# classes and converting them to a 'stream' which you can then write / read from disk.
Upvotes: 0