Josh Smith MVVM Demo app - c#

In MainWindow we have:
<HeaderedContentControl
Content="{Binding Path=Workspaces}"
ContentTemplate="{StaticResource WorkspacesTemplate}"
Header="Workspaces"
Style="{StaticResource MainHCCStyle}"
/>
In the resources:
<DataTemplate x:Key="WorkspacesTemplate">
<TabControl
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ClosableTabItemTemplate}"
Margin="4"
/>
</DataTemplate>
And in the article says:
A typed DataTemplate does not have an x:Key value assigned to it, but
it does have its DataType property set to an instance of the Type
class. If WPF tries to render one of your ViewModel objects, it will
check to see if the resource system has a typed DataTemplate in scope
whose DataType is the same as (or a base class of) the type of your
ViewModel object. If it finds one, it uses that template to render the
ViewModel object referenced by the tab item's Content property.
My question is:
How does the template know that the type is a collection of workspaces (WorkspaceViewModel)?

It doesn't need to, in the code you've posted. In your sample, you have given a strict value to your content template: you've explicitly used {StaticResource WorkspacesTemplate}, and so a resource with the key of "WorkspacesTemplate is looked up.
Because you've explicitly set the template, it doesn't matter what the intended type is: it'll try to display any object in your Content using the template you've set - with varying degrees of success if you use a type that doesn't match!
In the alternate method you mention - with a "typed DataTemplate", you would declare your datatemplate with <DataTemplate DataType="{x:Type l:WorkSpace}" />. Note that there is no x:Key (and also that I've assumed you have a namespace l mapped to your local code). What happens here is that WPF automatically sets the key of your resource to the DataType (important to note: a resource key doesn't have to be a string!).
Then, when you declare your HeaderedContentControl, you can leave out setting the ContentTemplate. At runtime, when the control is rendered, WPF will check the type of the Content object and find that it is WorkSpace, and it'll then look up a resource with x:Key="{x:Type l:WorkSpace}" - which will match your typed template.
This is a useful way of making consistent representations of data throughout your application, since a typed DataTemplate will be used automatically by any content-presenting control throughout your application.

WPF doesn't really care about the concrete type, it's just need to be some IEnumerable of something, WPF uses the type descriptor to know what the ui binding with.

Related

How do you embed a template as content in XAML?

Is there any way to declare a top-level window as a content template in xaml and recreate it at load time? Essentially I'm trying to do something equivalent to data templating:
<DataTemplate DataType="{x:Type my:MyType}" >
<Window>
...etc...
</Window>
</DataTemplate>
This works fine at run-time but it generates an error at compile time due to the fact that you're not supposed to use a top-level window to a data template or style etc, so basically I need to replace DataTemplate with a custom class to get around this. I don't actually need support for DataType...data templates set the resource key implicitly to DataType but a bug in WPF means you can't use the relevant attribute on your own types. I can easily get around that by doing things in reverse i.e. specifying the type as the key:
<MyClass x:Key="{x:Type my:MyType}" >
<Window>
...etc...
</Window>
</MyClass>
What I need to know though is how to declare MyClass so that the XAML content generates a template that I can create instances from rather than an actual instance of the Window itself.

How does binding elementname work exactly?

I remember reading a couple of weeks ago that it sometimes doesn't work inside templates, and I recently tried to bind things in two different windows and it couldn't find the name declarations, so I assumed that it was local to the namespace of the class and just bound by setting the datacontext instead. However, I'm really curious when I am able to use binding elementname and when I cannot, because it's far more convenient when it is possible.
edit: In reading that article, I found this to be interesting:
"For this reason, styles and templates both define their own XAML namescopes, independent of whatever location in an object tree where the style or template is applied."
if this is true, doesn't that mean that Binding ElementName should not work in templates at all? But then I definitely have some working bindings on ElementName within my templates. That is the most confusing part, why do some bindings randomly work inside the templates and others do not? It must have some method for trying to resolve the name even if it isn't in the template or same namescope
Basically you need to be in the same name scope (read this). Most UI elements are in the same tree sharing the same name scope, however there can be breaks and barriers (styles/templates) and if you have abstract objects like DataGrid columns they do not have a name scope at all.
I've been working with WPF long enough to guess when i'll run into problems and i know common areas but don't think there's an easy way to tell in all situations up-front.
if this is true, doesn't that mean that Binding ElementName should not work in templates at all? But then I definitely have some working bindings on ElementName within my templates.
Within is just fine, that is the same scope. The point here is that if you apply the template and they would not have their own scopes there would be conflicts.
e.g.
<Button/>
<Button/>
If we expand the ControlTemplate you would get something like:
<Border Name="bd" Background="{TemplateBinding Background}">...</Border>
<Border Name="bd" Background="{TemplateBinding Background}">...</Border>
Obviously we would get a name conflict.
Same for DataTemplates in ItemsControls, if you name controls in the template that name would conflict with the same control instance in the applied template of other items.
On another note, you can bind from inside a template to the outside because logically there can only be one instance with that name or you can give them a distinct precedence based on how "close" the name scope is, e.g.
<TextBox Name="tb" Text="Test"/>
<ItemsControl ItemsSource="ABC">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Text, ElementName=tb}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

Resources and ContentControl templates

When setting a contentcontrols template to xaml in code behind I cant access a static resource contained in the parent xaml.
I have a contentcontrol as follows:
<ContentControl x:Name="ccMaterial">
<ContentControl.Resources>
<x:Array x:Key="BondListKey" Type="sys:Int32"
xmlns:sys="clr-namespace:System;assembly=mscorlib" />
</ContentControl.Resources>
</ContentControl>
then in codebehind I am setting the template as follows:
string template = "<ControlTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>" +
"<ComboBox Grid.Column=\"1\" Grid.Row=\"0\" ItemsSource=\"{Binding Source={StaticResource BondListKey}}\" />" +
"</ControlTemplate>";
ccMaterial.Template = (ControlTemplate)XamlReader.Parse(template);
The problem is that when i try to run this I get the exception saying that the resource "BondListKey" cannot be found. Can anyone explain why?
Please let me know if you need anymore information.
In response to Johns comments :
I have a tab item and I want to be able to display different controls within that tab based on a user selection somewhere else on the form. As an example if the user selected a car I would like to be able to change the control template to include a textbox for engine size, fuel type etc, if the user selected an orange I would like a control template that included variety and sweetness. I suspect I could get this functionality by drawing all possible controls on the tab, then altering the visible/enabled state of the relvant controls based on a datatrigger, but this would potentially involve a LOT of filtered controls ( as there may be many user selection types ). What I ideally want to be able to do is have the desired control template supplied as a string, parsed and assigned to the template of the control, thus modifying its contents at runtime.
Please let me know if that didnt make sense or you need anythign clarifying :)
StaticResource is a static lookup that is executed once at load time. If the target resource is not found at that time, you get an error, which is you're seeing now. Because you're loading the template in the context of the XamlReader the resources in your XAML aren't available. In most cases the fix is to use DynamicResource instead to provide a default value that gets updated when the resource becomes available, but Binding Source is not a DependencyProperty and so can't use Dynamic.
Rather than using a XamlReader, you can just declare your XAML in XAML and take advantage of the context that's available there:
<ContentControl x:Name="ccMaterial">
<ContentControl.Resources>
<x:Array x:Key="BondListKey" Type="sys:Int32"
xmlns:sys="clr-namespace:System;assembly=mscorlib" />
<ControlTemplate x:Key="MyTemplate">
<ComboBox Grid.Column="1" Grid.Row="0" ItemsSource="{Binding Source={StaticResource BondListKey}}" />
</ControlTemplate>
</ContentControl.Resources>
</ContentControl>
You can then still do the loading from code with:
ccMaterial.Template = ccMaterial.FindResource("MyTemplate") as ControlTemplate;

DataContext in WPF

I am trying the following in my WPF application:
Structure of XAML elements.
DataTemplate[Data Type 'A']
Grid
Broder
TextBlock
I want to bind the text block's text property to a "string" which is derived from my "Users" class which is referenced in the resource dictionary of the XAML.
So in the above structure since the DataTemplate gets the feed from data type 'A'.
I want to assign the datacontext(Users) to the grid and bind the string to the textblock.
Is there a way i can achieve this ,since all my trials which include assigning the datacontext to the Grid or Border or TextBlock doesn't work.
Can any one suggest me or correct me if my approach is wrong here ?
This markup should suffice:
<DataTemplate DataType="{x:Type local:A}">
<Grid DataContext="{Binding Path=Users}">
<Border>
<TextBlock Text="{Binding Path=PropertyOnUsers}"/>
</Border>
</Grid>
</DataTemplate>
Make sure you have the namespace declared at the top of your Xaml. For whatever reason, WPF doesn't always automatically infer the template from the type if you don't use {x:Type ...}.
From there it should be straight forward.
If Users is a collection, you will have to drill into the collection to get a specific instance of User.
By the way, if you are using Visual Studio, you can use the Output window to debug binding issues.

WPF Custom Control - ItemsControl template not being applied

I'm building a custom WPF control that derives from TabControl. In the ControlTemplate, I'm using a ItemsControl to display a list that is being bound from the template (an observable collection of type FileMenuItem). During program execution, I'm getting the following error in the output window:
ItemTemplate and ItemTemplateSelector
are ignored for items already of the
ItemsControl's container type;
Type='FileMenuItem'
The type FileMenuItem is derived from MenuItem. If I change the base class to DependencyObject, the code actually runs and the template is applied (so that's an option). I googled the error and couldn't find anything about it, has anyone run into this while developing custom controls? Even though I have a workaround, I'd like to understand what's happening, and I think using the MenuItem as a base class is a cleaner implementation.
I can post more code if it would help. Thanks!
The purpose of a DataTemplate (like ItemTemplate) is to provide a visualization for a data object. Specifically, it defines a set of elements to add to the visual tree in place of the data given to an ContentPresenter or ItemsPresenter. In your case your source list is a collection of objects that are already able to be added directly to the visual tree for display in the UI.
You can see this in the following simplified example where only "Three" shows up in Red because the first two items are defined in a form that can be displayed directly by ComboBox.
<ComboBox>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Foreground="Red"/>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBoxItem>One</ComboBoxItem>
<ComboBoxItem>Two</ComboBoxItem>
<sys:String>Three</sys:String>
</ComboBox>

Categories

Resources