Why can't I find DataTemplates in merged resource dictionaries? - wpf

In my MainWindow.xaml, I have the following reference to a ResourceDictionary:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
In MainSkin.xaml, I define a datatemplate:
<DataTemplate x:Key="TagTemplate">
...
</DataTemplate>
Deeper within my application, I attempt to use this data template:
<ContentControl DataContext="{Binding Tag}" ContentTemplate="{StaticResource TagTemplate}"/>
The code compiles successfully, but when I attempt to load a Page or UserControl that contains this StaticResource, I get an exception saying that the TagTemplate can't be found.
What am I doing wrong?

In order to access the contents of a resource defined in a XAML file, you need to "include" that XAML file in each page and control that uses it. So every XAML files will need to have the MergedDictionaries entry that you have in MainWindow.xaml.
Alternatively you can add those merge dictionaries to App.xaml and those resources are included implicitly:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

Are you using that StaticResource in the same Window where it is declared? Otherwise I think that you cannot have access to that.

Related

How can I access a dynamic resource from a different assembly inside a ViewBase

I am working with a Xaml file that is a custom view derived from a ViewBase, and I would like to access a DynamicResource that is in a different assembly. I have seen that it is possible to use something like:
<Application.Resources>
<ResourceDictionary
Source="/mylib;Resources/MyStyleDictionary.xaml" />
</Application.Resources>
However I'm dealing with a xaml file that looks something like:
<myLib:ViewBase>
<Grid>
<Button>
Style="{DynamicResource MyButtonStyle}" // I want this style to come from a different assembly
</Button>
</Grid>
</myLib:ViewBase>
How can I do this?
It's important to understand the difference between Dynamic and Static resources. What's the difference between StaticResource and DynamicResource in WPF?
But to answer the question:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/SomeOtherAssembly;Resources/SomeOtherDictionaryWithMyButtonStyleKey.xaml" />
<ResourceDictionary Source="/mylib;Resources/MyStyleDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
The resource is being referenced dynamically, so merging SomeOtherDictionaryWithMyButtonStyleKey.xaml before merging in MyStyleDictionary.xaml should work.

Referencing Resource Dictionaries in a separate project

I've recently split my silverlight application into several smaller projects.
I've moved all of the resource dictionaries containing my styles into a separate project ("Application.Themes") I then reference these from my App.xaml file within my main project.
This works fine for the main project however all other projects that reference styles within these resource dictionaries throw "Object Reference not set to an instance of an object" exceptions within the designer, although they do compile and run without any problems and with the correct styles.
I've added an App.xaml file to each of the individual projects which references the same dictionaries as my main App.xaml file, this has made no difference.
Is there a correct way to reference resource dictionaries from another project which allows the designer to be used?
EDIT:
Here is some more information and some code snippets to demonstrate the issue I'm having
I have a styles project called "Themes" within this project I have several dictionaries that define all of the styles for the project.
Within my main App.xaml I have the following
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes;component/Styles/CoreStyles.xaml"/>
<ResourceDictionary Source="/Themes;component/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
If I reference styles within the main project they work correctly. however they don't for any other projects even if those projects reference the Themes project.
I've attempted to put the following at the start of each UserControl in order to resolve the styles at design time, however it still cannot resolve styles that are within the project.
<UserControl>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes;component/Styles/CoreStyles.xaml"/>
<ResourceDictionary Source="/Themes;component/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Additional Control Specific resources -->
</ResourceDictionary>
</UserControl.Resources>
<!-- The following resources are defined in Styles.XAML and don't resolve at design time and throw errors -->
<TextBlock Text="Header Test"
FontFamily="{StaticResource HeaderFontFamily}"
Foreground="{StaticResource StrongBrush}">
</UserControl>
My styles.xaml looks similar to this.
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:behaviors="clr-namespace:Minerva.Presentation.Behavior;assembly=Minerva.Presentation"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
<SolidColorBrush x:Key="StrongBrush" Color="{Binding Source={StaticResource MetroColors}, Path=Palette.StrongColor}" />
<FontFamily x:Key="HeaderFontFamily">Segoe UI Light, Lucida Sans Unicode, Verdana</FontFamily>
</ResourceDictionary>
Create an assembly for the styles/themes project so that this can be referenced by other projects.
In order to merge these styles into application either at app.xaml/page.xaml using MergedDictionaries
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Assembly;component/Stylesorthemes.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Hope this is useful.
I created an assembly test which contains the resource dictionary Theme.xaml
Theme.xaml code
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Thickness x:Key="GeneralThickness">10</Thickness>
</ResourceDictionary>
I created seperate silverlight project testreturns
case1.In App.xaml
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="testreturns.App"
>
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/test;component/Theme.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
case2.Usercontrol level
<UserControl.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/test;component/Theme.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
And using it to set borderthickness of button
<Button Height="50" Width="150" BorderThickness="{StaticResource GeneralThickness}"/>
In both cases it is working for me.
Is this what you intend for?
Did you set the theme.xaml file properties BuildAction to Resource while creating assembly?

ResourceDictionary in application resources cannot be found

I'm facing some strange issue with a ResourceDictionary in my Application.Resources.
In order to fill multiple ComboBoxes inside my application, I want to store the data in a ResourceDictionary.
However, I get the "Cannot find a Resource with that Name/Key..." error constantly.
My XAML-Code here:
<Application.Resources>
<ResourceDictionary x:Key="RDArray">
<sys:String x:Key="item1">Item1</sys:String>
<sys:String x:Key="item2">Item2</sys:String>
<sys:String x:Key="item3">Item3</sys:String>
</ResourceDictionary>
</Application.Resources>
<ListBox x:Name="lb" ItemsSource="{Binding Values, Source={StaticResource RDArray}}" />
Due to some lucky circumstances I was able to find out that putting another resource like Style above the Dictionary solves the problem.
<Application.Resources>
<Style x:Key="fubar" />
<ResourceDictionary x:Key="RDArray">
<sys:String x:Key="item1">Item1</sys:String>
<sys:String x:Key="item2">Item2</sys:String>
<sys:String x:Key="item3">Item3</sys:String>
</ResourceDictionary>
</Application.Resources>
The "bug" occurs in a WPF application as well as in Silverlight.
Although I can solve this using the shown "trick", I am curious where this error is coming from. I wasn't able to find anything about this. Maybe it is just me and something I am understanding wrong about resources in WPF.
This is because if you have a resource dictionary as the only item in the resources section then the contents simply get added to the parent dictionary (I snooped and this seems to be the case). To get around this you need to put your resource dictionary in separate xaml file (List.xaml in this case):
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary x:Key="RDArray">
<sys:String x:Key="item1">Item1</sys:String>
<sys:String x:Key="item2">Item2</sys:String>
<sys:String x:Key="item3">Item3</sys:String>
</ResourceDictionary>
</ResourceDictionary>
and then reference that in your main app:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="List.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<ListBox x:Name="lb" ItemsSource="{Binding Values, Source={StaticResource RDArray}}"/>
Thanks for your answer.
In my real application I created that separated file, but I didn't make a second ResourceDictionary inside of it a nd just put the string values there. So it looked like:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<sys:String x:Key="item1">Item1</sys:String>
<sys:String x:Key="item2">Item2</sys:String>
<sys:String x:Key="item3">Item3</sys:String>
</ResourceDictionary>
So this was of course not working either.
Thanks for your answer, definitely solves this one.

Add collection or array to wpf resource dictionary

I've search high and low and can't find an answer to this. I have two questions
How do you create an array or collection in XAML. I've got an array I want to stick in there and bind to a combo box. My first idea was to put an ItemsControl in a resource dictionary, but the ItemsSource of a combo box expects IEnumerable so that didn't work.
Here's what I've tried in my resource dictionary and neither works
<ItemsControl x:Key="stateList">
<sys:String>AL</sys:String>
<sys:String>CA</sys:String>
<sys:String>CN</sys:String>
</ItemsControl>
<ItemsControl x:Key="stateList2">
<ComboBoxItem>AL</ComboBoxItem>
<ComboBoxItem>CA</ComboBoxItem>
<ComboBoxItem>CN</ComboBoxItem>
</ItemsControl>
and here's how I bind to it
<ComboBox SelectedValue="{Binding Path=State}" ItemsSource="{Binding Source={StaticResource stateList2}}" >
</ComboBox>
EDIT: UPDATED
I got this first part to work this way
<col:ArrayList x:Key="stateList3">
<sys:String>AL</sys:String>
<sys:String>CA</sys:String>
<sys:String>CN</sys:String>
</col:ArrayList>
However, I'd rather not use an array list, I'd like to use a generic list so if anyone knows how please let me know.
EDIT UPDATE: I guess XAML has very limited support for generics so maybe an array list is the best I can do for now, but I would still like help on the second question if anyone has an anser
2nd. I've tried referencing a merged resource dictionary in my XAML and had problems because under Window.resources I had more than just the dictionary so it required me to add x:Key. Once I add the key, the system can no longer find the items in my resource dictionary. I had to move the merged dictionary to Grid.Resources instead. Ideally I'd like to reference the merged dictionary in the app.xaml but I have the same problem
Here's some sample code. This first part required an x:key to compile because I have converter and it complained that every item must have a key if there is more than one
<UserControl.Resources>
<win:BooleanToVisibilityConverter x:Key="VisibilityConverter" />
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ResourcesD.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
I had to change it to this
<UI:BaseStep.Resources>
<win:BooleanToVisibilityConverter x:Key="VisibilityConverter" />
</UI:BaseStep.Resources>
<Grid>
<Grid.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ResourcesD.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Grid.Resources>
</Grid>
Thank you
As far as I understand your problem you want to bind a ComboBox (or a ListBox) with an array of items, right? If you want items from some external data source, you can just make use of handy DataContext property. Search MSDN for more on this. However, if you do want a manual collection, do it this way:
Create your own class:
public class StringCollection : ObservableCollection<string> { }
and now use it like this:
<Window.Resources>
<local:StringCollection x:Key="stringCollection">
<sys:String>Hello</sys:String>
<sys:String>World</sys:String>
</local:stringCollection>
</Window.Resources>
...
<ListBox
Margin="15"
ItemsSource="{StaticResource stringCollection}" />
Or, if you want more generic collection create a class like this:
public class ObjectCollection : ObservableCollection<object> { }
and use it like this:
<local:ObjectCollection x:Key="objectCollection">
<sys:String>Hello</sys:String>
<TextBlock>World</TextBlock>
<sys:Int32>12345</sys:Int32>
</local:ObjectCollection>
...
<ComboBox
Margin="15"
ItemsSource="{StaticResource objectCollection}" />
You may also want to make use of some in-built classes like Int32Collection that implements IEnumerable. You can use them directly as a resource.
The Resources property (of any FrameworkElement like Window, UserControl, or Application) is of type ResourceDictionary not collection of resource dictionaries! so you can't do like this:
<UserControl.Resources>
<!-- The first RD -->
<!--<ResourceDictionary>
<win:BooleanToVisibilityConverter x:Key="VisibilityConverter" />
</ResourceDictionary>-->
<!-- Second RD!!! -->
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ResourcesD.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Instead do like this:
<UserControl.Resources>
<!--
There should be only 1 main RD,
Merge other RDs, if any
-->
<ResourceDictionary>
<!-- First Resource -->
<win:BooleanToVisibilityConverter x:Key="VisibilityConverter" />
<!-- Second Resource -->
<ResourceDictionary.MergedDictionaries>
<!-- Now, there can be multiple RDs -->
<ResourceDictionary Source="/ResourcesA.xaml" />
<ResourceDictionary Source="/ResourcesB.xaml" />
<ResourceDictionary Source="/ResourcesC.xaml" />
<ResourceDictionary Source="/ResourcesD.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Hope this helps.
Regards,
Mihir Gokani
Please correct me if I'm wrong but I think you want something like this?
<Grid.Resources>
<local:MyCustomCollection x:Key="local:MyCustomCollection"/>
</Brid.Resources>
At the top of your window you'd have a property:
xmlns:local="clr-namespace:MyNamespace"
And inside your code you'd have:
MyCustomCollection custCol = MyGrid.Resources["MyCustomCollection"] as MyCustomCollection;
Binding would happen on a control with a property something like this:
ItemsSource="{StaticResource MyCustomCollection}"
For the resources, you just need to mover your additional converter into the newly declared ResourceDictionary:
<App.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ResourcesD.xaml" />
</ResourceDictionary.MergedDictionaries>
<win:BooleanToVisibilityConverter x:Key="VisibilityConverter" />
</ResourceDictionary>
</App.Resources>
If you add resource dictionary inside Window's resources, you will not be able to access it everywhere, instead you should add in "App"'s resources (check out App.xaml file).
<App.Resources>
<win:BooleanToVisibilityConverter x:Key="VisibilityConverter" />
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ResourcesD.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</App.Resources>
This will be available in every WPF object regardless of creating any inheritance hierarchy.

How to add more than one resource to a XAML window?

I have a little problem right now and I don't know how to fix it. I want to add two resources to a window. One is a XAML File style resource, the other a ValueConverter Class.
Both of them work if I use only one resource at a time:
<Window.Resources>
<ResourceDictionary Source="Resources\MyStyles.xaml" />
<Window.Resources>
or
<Window.Resources>
<local:MarginConverter x:Key="adjustMargin"/>
</Window.Resources>
But if I try something like this:
<Window.Resources>
<local:MarginConverter x:Key="adjustMargin"/>
<ResourceDictionary Source="Resources\MyStyles.xaml" />
</Window.Resources>
I get the message the resource is already been set and can not set twice.
I have no idea how to get this done. Is there something like a resource group?
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources\MyStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<local:MarginConverter x:Key="adjustMargin"/>
</ResourceDictionary>
</Window.Resources>

Resources