DataTemplate to populate the content of other DataTemplate - wpf

I've like 'base' DataTemplate that contains TabControl with 3 tabs. In each tab I put empty ContentPresenter.
I want to write new DataTemplate for each derived type. In which, I want to populate the all 3 ContentPresenter.
How can I reffer to each ContentPresenter of the base template so I can put data inside?

I assume you have set content of each tabitem.
If the content objects have different types you just have to make a datatemplate for each of the types.

Related

Which WPF template to use?

I'd like to create a re-usable ListView with rows of any or all of:
Labels + textboxes
or
Labels + Comboboxes
or
Labels + DatePickers
using Templates. I still do not understand Templates too well and would like to know which of them - ControlTemplate, DataTemplate, ItemsTemplate or ContentTemplate - to use for this and how. Thanks!
There are 2 different types of templates: DataTemplate and ControlTemplate. ControlTemplate is used on the Template property of classes derived from Control and defines the visual tree for a specific type of control.
Pretty much any other place that templates show up is using DataTemplate. This includes ContentTemplate and ItemTemplate properties. DataTemplates define a visual tree for any non-Visual data type. When the template is rendered its DataContext is the data object being rendered (i.e. a List<T> item) making it easy to bind data properties.
To mix templates for different types in a single list you can use a DataTemplateSelector which allows you to write code to pick a template for each item. The other option is to create multiple implicit templates (DataType but no x:Key) for the different CLR types of objects in the list. As long as those templates are in the resource scope of the control rendering the collection the types will resolve their templates automatically.

Any ideas how to implement ComboBox to represent hierarchical data?

Does anybody saw control like this somewhere?
I need to make such control to represent hierarchical data (it should be generic very likely, i.e. data binding, templates support).
Something like combination of ComboBox and MenuItem’s.
I think I will redefine the combobox itemtemplate with some hierarchicaldatatemplate along with popup class.
Just put ComboBoxes on a form and bind the ItemsSource to the top level collection.
Then bind the DataContext of the next ComboBox to the SelectedItem of the box on the left and bind its ItemSource to the collection of items.
You know how to bind to SelectedItem?
E.G.
Column1
Public String Name
Public List Column2s
So you bind the first combox to List with he displaymemberpath = name
Then on the second combobox you bind to Column1 selecteditem with items source path of Column2s
The trick is to build up the Lists within the Lists within the Lists
All right, I made it by custom control inherited from ComboBox, custom ComboBoxItem inherited from HeaderedItemsControl and using HierarchicalDataTemplate.

Selecting TabItem from code-behind when tabs use a DataTemplate

I've got a TabControl in my WPF application. The TabControl's ItemsSource is bound to an ObservableCollction of view objects. It uses a DataTemplate to populate the visual tree for the tabs from the Items in the collection.
I need to be select the current tabs in the conde-behind in response to actions the user takes on another screen in the application. When I iterate over the items in the TabControl's Items collection, I get the instances of my view models.
How do I access the actual TabItems and iterate over them, then select the one I want?
Tony
If you're using a MVVM approach you should bind your TabControl's SelectedItem property to the same object that holds your ObservableCollection of TabItems (the ViewModel). When you need to change the current tab set the SelectedItem property to the correct TabItem in the ObservableCollection.

Why do I need ContentPresenter in SilverLight?

If I can replace it with a single TextBox (like on a button),
or I can add media element directly to Grid (whatever)...
What is ContentPresenter for? Is there some advantages?
You don't always need a ContentPresenter. It acts as a placeholder that will effectively host any content that you assign to its Content attribute. If you have an area on a given control/page that can hold dynamic content of an indeterminate type, a ContentPresenter is an effective way to hold the space.
It's also used quite a bit with templating, custom controls, etc. Odds are you won't actually use it until you start getting into some fairly advanced stuff.
One kind of cool thing you can do is have the Content attribute of the ContentPresenter bound to a DependencyProperty of type UserControl, and then if you set that DependencyProperty equal to any UserControl (like one that you new up in a ViewModel or something), it'll show up in that spot.
You typically use the ContentPresenter
in the ControlTemplate of a
ContentControl to specify where the
content is to be added. Every
ContentControl type has a
ContentPresenter in its default
ControlTemplate.
From MSDN; so basically it's a placeholder for content in a template.
It is used by ContentControl. Inside ContentControl's template, a ContentPresenter indicates as a placeholder where the actual content will be placed.
From MSDN,
Displays the content of a ContentControl.

WPF control with many GridViews

I have a WPF user control that will dynamically have any number of GridViews. Each is essentially the exact same except for the ItemsSource. Therefore each have the same columns and the same RowDetailsTemplate. To be specific what I am actually doing is settings the columns to the bound values and then setting the RowDetailsTemplate to the data template.
What is the best approach to essentially define the GridView once and copy it to all the others. I have tried XamlWriter.Save with the GridView in the resources, but the columns and RowDetailsTemplate are not saved. I have also tried a style, but the columns property is not settable.
Maybe I did something wrong with the two approaches.
Use an ItemsControl with ItemTemplate. The DataTemplate used for the Itemtemplate should contain the GridView XAML with all the column definitions. Next bind the ItemsControl's ItemSource to a collection of whatever data object you want the GridViews to use as ItemSource.

Resources