WPF - Defining DataTemplate inside ResourceDictionary without a key - wpf

I have seen many times wpf code samples in this form:
<Window.Resources>
<DataTemplate DataType="{x:Type SomeType}">
<!-- Elements defining the DataTemplate-->
</DataTemplate>
</Window.Resources>
I understand the usage, but I cant understand why is this syntax is ok: since ResourceDictionary implements IDictionary, therefore every element that we add to Resource property must specify a key. Now I know that using the DictionaryKeyPropertyAttribute, a class can provide an implicit key value - but in case of DataTemplate class, the provided property is "DataTemplateKey". I know it sounds a bit petty, but the motivation for this question is to know how to use other classes even if I didnt have the privilege to see usage samples before (maybe some 3rd party...). anyone?

As you mentioned in your question, entries without an x:Key attribute use DataTemplateKey(SomeType) as the key. You can only specify one such instance for a particular SomeType in the resources. DataTemplateKey is derived from TemplateKey which itself is derived from ResourceKey. Of course such DataTemplate resource definitions can appear for many types, staying unique, because the DataTemplateKey of each respective type will be unique.
For example, consider the following Resources definition:
<Window.Resources>
<!-- Generic Button data template -->
<DataTemplate DataType="{x:Type Button}">
<!-- Elements defining the DataTemplate-->
</DataTemplate>
<!-- Generic TextBlock data template -->
<DataTemplate DataType="{x:Type TextBlock}">
<!-- Elements defining the DataTemplate-->
</DataTemplate>
<!-- Specific Button data template -->
<DataTemplate x:Key="SpecialButton" DataType="{x:Type Button}">
<!-- Elements defining the DataTemplate-->
</DataTemplate>
</Window.Resources>
This results in three entries in the resource dictionary. The orange arrows in the image, below, point to the DataTemplateKey based entries for the Button and TextBlock types, while the red arrow points to the specific (keyed) entry for the SpecialButton keyed resource:

Related

WPF Window.Resources - ambiguous reference

I'm trying to add some resources to my MainWindow.xaml file but I cant seem to open the tag Window.Resources due to ambiguous reference error.
This hints to me that I have some assembly that also has the name Resources, but that is not the case, I don't have any folders/classes with that name. What could be causing that? Or rather what are the other "references" causing the compiler to be unsure which one to select?
Window.Resources is an example of the special property element syntax that can be used in XAML.
Specifically, the syntax begins with a left angle bracket (<),
followed immediately by the type name of the class or structure that
the property element syntax is contained within. This is followed
immediately by a single dot (.), then by the name of a property, then
by a right angle bracket (>). As with attribute syntax, that property
must exist within the declared public members of the specified type.
I've emphasized the important point that causes the error.
What you're currently doing, is trying to assign something to a property Resources in a type Window but in an instance of a Grid.
So you should either place your resources in the Window instance like this:
<Window>
<Window.Resources>
<!--your resources here-->
</Window.Resources>
<Grid>
<!--your grid content here-->
</Grid>
</Window>
...or you should place your resources in the Grid:
<Window>
<Grid>
<Grid.Resources>
<!--your resources here-->
</Grid.Resources>
<!--your grid content here-->
</Grid>
</Window>

Hierarchy of applying multiple DataTemplates to a single DataType

I've seen some allusions to this question around SO, but I haven't found a specific answer yet.
Say I have models, ObjectA and ObjectB, that inherit from BaseObject and have corresponding ViewModels, ObjectAViewModel and ObjectBViewModel. Each is the DataType of a DataTemplate that is stored in a ResourceDictionary that represents how they are typically displayed in a View.
However, in certain circumstances, based on the View's design, I want to use a different DataTemplate for ObjectA that is stored in another ResourceDictionary, but still use the same DataTemplate for ObjectB. Is this is situation where the order of the resource dictionaries matters?
I am in the process of refactoring an old project into WPF, and I am about a day or two away from structuring this part of the program. So, I'll come up with an answer on my own in a couple days, but in case someone catches this sooner, I figured I wasn't the only one wondering about this. What follows is the structure I have in my head for how this could work.
class BaseObject { }
class ObjectA : BaseObject { }
class ObjectB : BaseObject { }
class BaseObjectCollection : ICollection<BaseObject> { ObjectA; ObjectB; }
class ObjectAViewModel { }
class ObjectBViewModel { }
<!-- Main Resources File -->
<ResourceDictionary>
<DataTemplate DataType="{x:Type ObjectAViewModel}" />
<DataTemplate DataType="{x:Type ObjectBViewModel}" />
</ResourceDictionary>
<!-- Displays ObjectA, ObjectB using above Main Resource File DataTemplates -->
<Window x:Name="ViewA">
<Window.Resources>
<...code for grabbing Main Resources File... />
</Window.Resources>
<ItemsControl ItemsSource="_baseObjectCollection" />
</Window>
<!-- Second Resource File -->
<ResourceDictionary>
<DataTemplate DataType="{x:Type ObjectAViewModel}" />
</ResourceDictionary>
<!-- What happens here? -->
<Window x:Name="ViewB">
<Window.Resources>
<...code for grabbing Main Resource File... />
<...code for grabbing Second Resource File... />
</Window.Resources>
<ItemsControl ItemsSource="_baseObjectCollection" />
</Window>
Yes, I could use x:Key's and a TemplateSelector, but I would prefer to use the automatic selection process.
Thanks.

how to prevent using global DataTemplate without specifiying new one

<DataTemplate DataType="{x:Type MyType}">
...
</DataTemplate>
I have a default DataTemplate for MyType.
want to prevent using it below without having to specify a real DataTemplate
<ItemsControl ItemsSource="{whateverList of MyType}" ItemTemplate="{x:Null}"/>
ItemTemplate="{x:Null}" doesn't get the job done -> shows default DataTemplate
would be happy with "ToString()" display
any ideas?
If you want to override the default data template for the type, you will have to specify a different one:
<ItemsControl ItemsSource="{listOfMyType}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- Whatever -->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
There is some more discussion about this approach here, including another's attempt at using {x:Null} for the template.
If your template needs to bind to the ToString() for your type (and no property exists in the type to do this for you), you'll need to use an IValueConverter, as discussed here.

WPF Dynamic Resource Lookup Behavior with FixedPage

Having the following very simple xaml:
<DocumentViewer Name="dv">
<FixedDocument Name="fd" Loaded="fd_loaded">
<FixedDocument.Resources>
<Style x:Key="TestStyle">
<Style.Setters>
<Setter Property="TextBlock.Foreground" Value="BlueViolet"/>
</Style.Setters>
</Style>
<SolidColorBrush x:Key="foregroundBrush" Color="Orange"/>
</FixedDocument.Resources>
<PageContent Name="pc">
<FixedPage Name="fp" Width="800" Height="600" Name="fp">
<TextBlock Name="tb" Style="{DynamicResource TestStyle}">
Lorem ipsum
</TextBlock>
<TextBlock Foreground="{DynamicResource foregroundBrush}" Margin="20">
Lorem ipsum
</TextBlock>
</FixedPage>
</PageContent>
</FixedDocument>
</DocumentViewer>
The use of Dynamic Resources (which I actually need in a more complex situation) here doesn't work. Using Static Resources colors the TextBlocks in the desired colors. Moving the Resources to the level of the FixedPage also does the trick. But I would like to have one generic resource dictionary on a top level element (because of runtime changes the user can make for colours, fonts, etc.). Placing the resources on Application level also does work. But it's not an option for good reasons.
Anybody have any clue why this doesn't work. Does it have something to do with the Logical Tree from the TextBlock upwards?
MSDN Resources Overview states that:
The lookup process checks for the requested key within the resource dictionary defined by the element that sets the property.
If the element defines a Style property, the Resources dictionary within the Style is checked.
If the element defines a Template property, the Resources dictionary within the FrameworkTemplate is checked.
The lookup process then traverses the logical tree upward, to the parent element and its resource dictionary. This continues until the root element is reached.
I also tried putting the Brush and the Style into the Resources of a (dummy) Style according to the above explanation of MSDN. But that didn't work either.
Really have the feeling that this can not be that complex, but most probably I oversee something. Any help is appreciated.
EDIT
When naming the TextBlock to "tb" and then using tb.FindResource("TestStyle") throws an exception. So that resource clearly can't be found. If I check out LogicalTreeHelper.GetParent(tb) and repeat that for the parents found I get the expected result: TextBlock > FixedPage > PageContent > FixedDocument ...
EDIT2
This works perfect. What's the difference with the XAML projected earlier?
<Window x:Class="WpfDynamicStyles2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<SolidColorBrush x:Key="resBrush" Color="Orange"></SolidColorBrush>
</Grid.Resources>
<StackPanel>
<Button>
<TextBlock Foreground="{DynamicResource resBrush}">Dummy text...</TextBlock>
</Button>
</StackPanel>
</Grid>
</Window>
EDIT3
private void fd_Loaded(object sender, RoutedEventArgs e)
{
Object obj = pc.TryFindResource("foregroundBrush");
obj = fp.TryFindResource("foregroundBrush");
obj = tb.TryFindResource("foregroundBrush");
}
The dynamic resource placed on the Foreground property of the textbox cannot be resolved (the actual resource is at the FixedDocument.Resources level). Also using the TryFindResource in code behind works from pc (PageContent) but from fp (FixedPage) and tb (TextBlock) it cannot resolve the resource (obj is null). When using a Static Resource in the XAML Markup everything works fine.
Well going the style way is a good design but not necessary. To make dynamic color based same named (keyed) brushes, we can use as dynamic color dictionaries (NOT brush dictionaries)
The solution can be as below ...
Create a single brush resource dictionary.
Refer the color in the brush with DynamicResource attribute.
Create multiple resource dictionaries with same keyed Color resource in each of them them.
Based on user's requirement, clear and add into Application.Current.resources.MergedDictionaries.
Example
Make a WPF project with a window that has following XAML....
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Window11Resources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<DockPanel LastChildFill="True">
<ComboBox DockPanel.Dock="Top" VerticalAlignment="Top"
SelectionChanged="ComboBox_SelectionChanged"
SelectedIndex="0">
<ComboBox.ItemsSource>
<Collections:ArrayList>
<System:String>Orange</System:String>
<System:String>Red</System:String>
<System:String>Blue</System:String>
</Collections:ArrayList>
</ComboBox.ItemsSource>
</ComboBox>
<DocumentViewer>
<FixedDocument>
<PageContent>
<FixedPage Width="793.76" Height="1122.56">
<TextBlock
FontSize="30"
Foreground="{StaticResource LabelColorBrush}"
Text="Test"/>
</FixedPage>
</PageContent>
</FixedDocument>
</DocumentViewer>
If you observe the window doesnt need to use anything which is dynamic. All refernces can remain static. So LabelColorBrush can be found in dictionary Window11Resources.xaml
In Window11Resources.xaml dictionary add a dynamic color brush.
<SolidColorBrush x:Key="LabelColorBrush"
Color="{DynamicResource DynamicColor}"/>
Add following 3 color brush dictionaries in some folder from your project...
<!-- Name = OrangeColorResource.xaml -->
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="DynamicColor">Orange</Color>
</ResourceDictionary>
<!-- Name = BlueColorResource.xaml -->
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="DynamicColor">Blue</Color>
</ResourceDictionary>
<!-- Name = RedColorResource.xaml -->
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="DynamicColor">Red</Color>
</ResourceDictionary>
Note that the key remains the same i.e. DynamicColor.
Now clear and recreate color dictionaries in App.Resources. I have done that in the code behind of Window.xaml.cs
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(
new ResourceDictionary()
{
Source = new Uri("Resources\\"
+ ((ComboBox)sender).SelectedItem.ToString()
+ "ColorResource.xaml",
UriKind.RelativeOrAbsolute) });
}
Now as and when you select a color from the drop down, the dynamic color changes on the static resource brush. Note that there is no Style involved.
I hope this answers what you are asking.
By the way: the reason for this post had some more complex background. I really wanted to use a single resource dictionary whereby I could change colors, fonts, fontsizes, etc. dynamically during runtime (by an end-user). I am working on a tax form application. And the tax forms presented on screen for user input are resolving their resources at the Application level. So far so good.
But at the same time I want to present the user a print preview of the tax form where the same resourcedictionary (as object type, not as object instance) is used to define color schemes, fonts, fontsizes, etc. to be used for printing. Which can differ from the styling used for on screen user input. That's why I wanted to "attach" the resource dictionary on (for example) the FixedDocument level so that all pages in the document would refer to it. And when changing colors, fonts etc. all pages with common elements (TextBlocks, TextEditors, etc.) would respond to the change.
And then I became stuck...as described in this post.
For now I have a nice workaround by creating a specific instance of the resourcedictionary, storing it as a private variable and attach it to every individual page I put in the fixed document. It works and that already pleases me. But my programmers heart is still a little ached, because I would prefer to use a single instance of the resource dictionary and put it at some top level control so all controls in it can use it.
As a programmer we have to live with workarounds as well ;) If you have any further suggestions, I'm open to receive them. Thanx for your support thus far.
Greetz, Joep.
Also see: MSDN Forum Post about this issue

Why is my typed data template not being applied?

I'm using Linq To Sql to fill up a listbox with Segment objects, where Segment is designer created/ORM generated class.
<Window x:Class="ICTemplates.Window1"
...
xmlns:local="clr-namespace:ICTemplates"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<DataTemplate x:Key="MyTemplate">
<!-- <DataTemplate DataType="x:Type local:Segment"> -->
// some stuff in here
</DataTemplate>
</Window.Resources>
<ListView x:Name="tvwSegments" ItemsSource="{Binding}" ItemTemplate="{StaticResource MyTemplate}" MaxHeight="200"/>
// code-behind
var queryResults = from segment in tblSegments
where segment.id <= iTemplateSid
select segment;
tvwSegments.DataContext = queryResults;
This works.
However if I used a Typed Data Template (by replacing the x:Key with the DataType attribute on the template, the items all show up with ICTemplates.Segment (the ToString() return value)
The concept is that it should pick up the data template automatically if the Type matches. Can someone spot the mistake here?
Ze Mistake is here
<DataTemplate DataType="x:Type local:Segment"> <!-- doesn't work -->
should be
<DataTemplate DataType="{x:Type local:Segment}">
Came home... made a toy-sample and it worked with this change. Gotta try that # work tomorrow. Sheesh.. for want of 2 curlies..
Update: Found out another gotcha
<DataTemplate x:Key="SegTemplate" DataType="{x:Type local:Segment}"> <!-- doesn't work -->
won't work. Seems like you can have it either with a Key OR DataType attribute. To make this typed data template work.. had to remove the Key attribute and now it works as expected. Behavior is consistent for a HierarchicalDataTemplate too.
<DataTemplate DataType="{x:Type local:Segment}">
This is just a guess, but could it be because the context is set to an IQueryable? If you set the DataContext to a single instance of a Segment, do you get the same result?

Resources