How to apply a custom style/template to a treeviewItem in xaml - wpf

So I have editted the TreeViewItem template, (so that I could get rid of the checkbox).
I was just wondering how I can apply this template to my treeview...The Treeview is databound.
is it something like:
<TreeView>
<TreeView.ItemTemplate>
....
...
?
Thanks!

Use the ItemContainerStyle of the TreeView. Or apply it implicitly by having it defined in the resources with only the TargetType set (this could be superior as the ItemContainerStyle may only affect the root items).

Related

WPF DataTemplate ContentPresenter "Binding"

Can someone explain me how the code I am using here can work?
<Window.Resources>
<DataTemplate DataType="{x:Type VM:PBRKEntryViewModel}">
<V:Overview />
</DataTemplate>
<DataTemplate DataType="{x:Type VM:LoginViewModel}">
<V:LoginView />
</DataTemplate>
</Window.Resources>
<Grid>
<ContentPresenter Content="{Binding CurrentView}"/>
</Grid>
My current problems in Details are:
Why can the ContentPresenter present the correct UserControl without Reference to the different DataTemplates? I can see, that ContentPresenter content is bound to my ViewModels CurrentViewProperty but my DataTemplates not?
Another great feature is that the UserControls using the correct ViewModels without a declaration. (Or without a declaration I can see)
I have found this description http://msdn.microsoft.com/en-us/library/System.Windows.Controls.ContentPresenter(v=vs.110).aspx but the remarks section has no answer to this questions. (Or I couldnĀ“t see them...)
Again and just for clarity everything is working perfect, but I do not understand why, so this is just a question to understand the Selection of the template and the Binding.
DateTemplates that specify a DataType property are automatically applied to any instance of that type in the view. It's just a way to tell WPF "every time you need to display this type, use this template"
Your ContentPresenter has its Content bound to some object. If that object type has a matching template, then WPF will use it.
Under the remarks section of the link you posted it's clear enough with this statement:
If there is a DataTemplate associated with the type of Content, the
ContentPresenter applies that DataTemplate to the Content property and
the resulting UIElement and its child elements, if any, are displayed.
Also, if you want to know how dataTemplates are picked automatically, you can read about it here - Data Templating Overview.
Quote from the link:
The DataTemplate class has a DataType property that is very similar to
the TargetType property of the Style class. DataTemplate gets applied
automatically to all objects associated with underlying type.
This is something similar to Styles. If you doesn't specify any x:Key on your Style it will be applied automatically to all child elements falling under the root element where resource is defined.
As soon as you set x:Key on Style, it is no more a default style and will be applied only to the elements explicitly setting style to this resource.
Same holds true for DataTemplate as well. When you specify DataType only, it becomes default template to represent underlying data type. Explicitly specifying x:Key will break this feature.

ContentControl inside a WPF TreeView

I am trying to add a content control inside a treeview, but when I add treeview items inside content control they are aligned further away from other treeview items.
<TreeView>
<TreeViewItem Header="XXX-1"></TreeViewItem>
<TreeViewItem Header="XXX-2"></TreeViewItem>
<ContentControl>
<TreeViewItem Header="YYY-1"></TreeViewItem>
</ContentControl>
<TreeViewItem Header="XXX-3"></TreeViewItem>
</TreeView>
The above code results in a treeview like below.
XXX-1
XXX-2
YYY-1
XXX-3
I think ContentControl adds another TreeViewItem by itself. How can I align the TreeViewItems together?
I would start with Rachel's answer but remove the ContentControl. You can accomplish this with two DataTemplates and an ItemTemplateSelector assigned to the TreeViewItem's ItemTemplateSelector property.
Define your complex and simple types into two DataTemplates. Then write a class which inherits from DataTemplateSelector that determines if the complex or simple type should be used in the TreeViewItem. You then set the TreeViewItem's ItemTemplateSelector to the DataTemplateSelector object you just created. Here's is an example: http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemtemplateselector.aspx.
Here is another link that shows you how to select a DataTemplate based on a condition: http://msdn.microsoft.com/en-us/library/ms742521.aspx. Search for the header Choosing a DataTemplate Based on Properties of the Data Object.
It does nest the ContentControl in a TreeViewItem (see Snoop screenshot below)
Snoop also tells me that the extra margin is for the +/- Expander, so you'll probably have to overwrite the template to get rid of that margin if you want to keep your TreeViewItems nested.
Of course, if you're simply trying to place a ContentControl inside your TreeViewItem, the tags should be the other way around.
<TreeViewItem Header="YYY-1">
<ContentControl />
</TreeViewItem>

Why is my TabControl ignoring my ItemsPanelTemplate?

I've used the ItemsPanelTemplate on other controls such as the ListBox, so I figured doing the same thing for the TabControl would be simple.
Apparently, I'm missing something and the TabControl is completely ignoring what I place in the ItemsPanelTemplate.
I have xaml that looks kinda of like this:
<TabControl TabStripPlacement="Right" ItemsSource="{Binding Components}">
<TabControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</TabControl.ItemsPanel>
</TabControl>
I've tried replacing WrapPanel with UniformGrid to see if there was a difference and it behaves the same. I do have a ControlTemplate for TabItem, but I tried removing it and it made no difference so I don't think that's effecting my problem.
You're probably looking to overwrite the Template, not the ItemsPanel
You can overwrite TabControl.ItemTemplate (or TabItem.Template) to alter the appearance of the Tabs along the top, TabControl.ContentTemplate to alter the template used for the content of the Tab, or TabControl.Template to alter the overall template of the TabControl.
I wasn't even aware that TabControl's had an ItemsPanel. I've only ever used that with an ItemsControl, where the ItemsPanel affects what kind of control contains the items in the collection. If the TabControl has that property, I expect it's only because it inherited it from some base class

Content generated from ContentTemplate does not have DataContext of Silverlight ContentControl set

In my Silverlight 4 application, I have a ContentControl with its ContentTemplate property bound to a property in the data context. That works fine. However, the content of the template once rendered has its DataContext set to null. I would like the content to inherit the same DataContext as set for the ContentControl. Is there a way to get this to happen?
The ContentControl's template has the ContentControl's Content property as a DataContext. So try
<ContentControl Content="{Binding}" />
if this is merely the current DataContext.
I found an alternate way to accomplish what was required. In my case, the template (not the content template) of the ContentControl was unimportant, so I made my DataTemplate objects into ControlTemplate objects instead and bound the Template property of the ContentControl instead of ContentTemplate. The data context was preserved if I did it this way.

Element addressing within a DataTemplate from a ContentControl

How do I access an element contained in a DataTemplate that is displayed through a ContentControl. I have a ContentControl which hosts a PresentationModel along the lines of:
<ContentControl x:Name="ContentContainer"
Content="{Binding}"
ContentTemplate="{Binding ContentControlTemplate, ElementName=this}"
Where "this" is the view (UserControl).
There's a DataGridControl I want to EndEdit on, so I tried this:
ContentPresenter presenter = VisualTreeHelper.GetChild(this. ContentContainer, 0) as ContentPresenter;
DataGridControl dg = this. ContentContainer.ContentTemplate.FindName("datagrid", presenter) as DataGridControl;
dg.EndEdit();
Problem is that the ContentControl has no children, maybe because of the way the content is bound?
I appreciate any help.
Well, you are casting the result of GetChild to a ContentPresenter. Depending on its Template, this may not be the case. I think its default template includes a Border, so your cast will return null. If you don't need it to do anything other than display the content, why not use ContentPresenter directly?

Resources