WPF HierarchicalDataTemplate - wpf

Could anyone explain how HierarchicalDataTemplate works
What Controls supports HierarchicalDataTemplate?
What does a control need to support HierarchicalDataTemplate?
UPDATE
What causes the TreeView to render
the parent and child nodes when the
same HierarchicalDataTemplate in a
HeaderedItemsControl only causes the
parent to be rendered?

What Controls supports HierarchicalDataTemplate?
All controls that inherit HeaderedItemsControl, such as TreeViewItem or MenuItem
What does a control need to support HierarchicalDataTemplate?
Inheriting from HeaderedItemsControl should be enough

Such control needs to be of type HeaderedItemsControl or derived from it. The current framework controls that do are MenuItem, ToolBar and TreeViewItem.
The HeaderedItemsControl overrides the PrepareContainerForItemOverride method and somewhere along that call path checks for HierarchicalDataTemplate.

Related

WPF: How to join business object and DataTemplate or ControlTemplate in a CustomControl

I'd like to create a CustomControl in WPF to present one to many items to the user in a certain manner.
For that I created a CustomControl with a dependency property ItemsSource and a dependency property ItemsTemplate. But I'm not sure how to join the business objects from the ItemsSource with the DataTemplate or ControlTemplate so that I have something WPFy to measure in the MeasureOverride and to place in the ArrangeOverride.
Do I use something like a ContentControl for each item and put my business object as DataContext?
Thank you for any advice or any push in the right direction.
After lots of googling and spying on the ItemsControl of WPF I think I have to use a ContentPresenter for each item which isn't a DependencyObject itself.

Expose inner controls in wpf

In wpf a ListBox has a ScrollViewer property and the intellisense shows it with a 'brackets' icon:
It allows you to navigate its inner properties:
In my custom control I want to expose an inner ListBox so I created a dependency property of type ListBox.
The result is a 'ListBox' property that shows up in the intellisense with a 'wrench' icon and non-navigable inner properties.
How can i mimic what the ListBox control does with the ScrollViewer?
ScrollViewer is a ContentControl which has these three Attached Properties
CanContentScroll
HorizontalScrollBarVisibility
VerticalScrollBarVisibility
So here ListBox sets these attached properties on itself that ScrollViewer exposes and only attached properties can be set like this.
Your case is different, you want to directly set/bind properties of child control on parent control which is not a best approach to follow. Rather you can expose properties you want you child to be bound to as dependency properties on custom control and bind child internally to those. In this way when you change those properties on custom control, child will also be updated.

UserControl child controls and FindName

I have a Silverlight UserControl which uses the ContentPropertyAttribute to exposes the Children property of one of it's child panels. This allows me to add child controls to the panel on my page:
<local:MyUserControl>
<TextBox Name="tbTest" />
</local:MyUserControl>
This works, apart from the 'tbTest' field of the page being present, but not initialised. On closer inspection, the InitializeComponent method does try to locate the TextBox (with FindName), but fails to do so (returning null).
After some investigation, I've found that namescopes are the problem - the UserControl has it's own namescope, thus it's children can't be located with the page's FindName but can with the UserControl's FindName method.
How can I alter my UserControl so that the child controls are locatable by the InitializeComponent method? The standard Panels (StackPanel, Grid, etc.) don't seem to have any problem doing so, so there must be a solution?
Thanks
It may be difficult to do at this point but the best course of action would probably be to derive your control from ItemsControl instead of UserControl. Then you wouldn't have the problem with name scopes.
I suppose as a workaround you could do a dive into the control with VisualTreeHelper to manually set the tbTest field.

WPF: Binding items added to UserControl's exposed children

I have a user control that allows items to be added to it by exposing a Grid's Children property. Any control I add shows up fine but when I try to bind a property on the added item to a control in the main window nothing happens (example):
<TextBox Name="txtTest" Text="Success!" />
<mycontrols:CustomUserControl.ExposedGridChildren>
<TextBox Text="{Binding ElementName=txtTest, Path=Text, FallbackValue=fail}"/>
</mycontrols:CustomUserControl.ExposedGridChildren>
This example always results in the TextBox's text showing "fail". Here is how I'm exposing the children in the user control:
public UIElementCollection ExposedGridChildren
{
get { return grdContainer.Children; }
}
Any thoughts? Is it a scope issue? I know I can't name the elements I add to the children because of scope errors. Thanks, Brian.
This might answer your question:
How do you bind a grid's children to a list?
Or as Dr. WPF puts it: (http://drwpf.com/blog/2007/10/15/itemscontrol-a-is-for-abundance/)
Is a Panel an ItemsControl?
No. The logical children of a panel
are UIElements, whereas the logical
children of an ItemsControl (its
Items) can be any CLR objects.
Sidebar: So what is a panel? The main
role of a panel is to provide layout
support for its children. Although a
panel does maintain a collection of
child elements, it is not technically
a WPF “control”… That is, it does not
derive from the Control base class and
it does not support the WPF notion of
templating. Instead, it is a single
element with a single purpose… namely,
to size and position (a.k.a., measure
and arrange) its children.
I was apparently going about this thing all wrong. What I needed to do was create a look-less control and template it to have one (or more) contentpresenter(s).

ListBox instead of ItemsPresenter in WPF Custom Control?

I'm writing a generic control template for my WPF Custom Control.
But with ItemsPresenter I only got raw list of Data..
Compared to the ListBox, the ListBox has all features I need.
Is it wrong to use a ListBox instead of ItemsPresenter ?
What I'm after is that
if I write a generic Template that uses a ListBox and in code behind I register some ListBox specific events and somebody overrides my generic Template with his own ControlTemplate witn an ItemsControl inside that does not possess that event, it will raise an Exception. In case of ItemsPresenter, everyone could use what he wants to.
Thanks.
I think you could add some test to see if the ItemsControl in the template is a ListBox or not. For example:
var itemsControl = this.Template.FindName("PART_Items", this);
if(itemsControl is ListBox)
{
// wire additional event handler here
}

Resources