DataTemplate in a separate ResourceDictionary - wpf

I know there a lot of topics related to this question but I could not find a solution that fits perfectly for my problem ... maybe there is none?
At the moment I have a UserControl which holds a navigation which allows the user to switch between different screens. These screens are defined in the Resources part of my UserControl as DataTemplate.
Something like that:
<DataTemplate TargetType={x:Type vm:ViewModel1}>
...
</DataTemplate>
<DataTemplate TargetType={x:Type vm:ViewModel2}>
...
</DataTemplate>
<DataTemplate TargetType={x:Type vm:ViewModel3}>
...
</DataTemplate>
Ok and what I wanna do is to place these DataTemplates in a separate XAML file and link this file to the UserControl's resources part. Do I really have to make this new XAML Resource Dictionary globally available in my application (adding it to the App.xaml resources) or is there another/better way?

No you do not have to make it global. Simply declare resource dictionary in your user control resources section just the same way as you did in app.xaml.
<Control.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Control.Resources>
You can point to file using relative file path "..\Folder\Folder\Dictionary.xaml" if you need to.

Related

Setting style on/in UserControl without having to define MergeDictionary?

I'm looking to set a style on some UserControls that's kept in a resource dictionary at the base of my project tree. The only way I've found to do so is below by defining MergeDictionary in the resources to link in the xaml file with styles. I'd prefer to not have to put this same code on every single control that needs this style though. Is there a better way to do this?
<UserControl
x:Class="TestApp.Screens.Sub.Details"
...
Style="{DynamicResource BottomContentUserControl}">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../../GeneralStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
</UserControl>
You can add the styles to the ApplicationResourceDictionary or you could add them to your App.xaml file so that they will be applied throughout your application.

Set window icon in style

I would like to use the same icon for my main window and for any dialogs or message boxes whithin my application, so I tried to set it like this in a ResourceDictionary:
<Style TargetType="{x:Type Window}">
<Setter Property="Icon" Value="pack://application:,,,/MyReferenceAssemblyName;component/Images/myIcon.gif"></Setter>
</Style>
But that does not work.
How could I share the same icon with the different windows?
Edit:
I have a simple resource dictionary (Style.xaml) where I am defining some global settings. I use it in my App.xaml like this:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/ViewModelTemplates.xaml"/>
<ResourceDictionary Source="Resources/Style.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
The file contains some definitions like e.g. button height, text box foreground color, etc.
There is no problem with those and all the panels and window my application creates use these settings. That is why I would like the icon to be defined there as well, to have it used allover the application.
I am not looking for a way to set the icon of the .exe file.
Edit:
I have not found the solution for what I want to do, so I ended up creating a BitmapImage in my ResourceDictionary and use it as DynamicResource in each of my Window-Classes.
<BitmapImage x:Key="ApplicationIcon" UriSource="pack://application:,,,/MyReferenceAssemblyName;component/Images/myIcon.gif"></BitmapImage>
and
<Window ...
Closing="Window_Closing"
Title="{Binding Title, UpdateSourceTrigger=PropertyChanged}"
IsEnabled="{Binding IsEnabled, FallbackValue=True, Mode=OneWay}"
WindowState="Maximized"
Icon="{DynamicResource ApplicationIcon}">
...
</Window>
For others that come across this issue, as I had the same one, see a similar question which provides a bit more of an explanation to why this doesn't work.
How to set default WPF Window Style in app.xaml?
There's also a couple of suggestions; one being tabina's solution and to apply the style to each Window separately. It includes an interesting approach to deriving the Window class, but it's down to personal preference, as they're only work-arounds.

Add items dynamically to ResourceDictionary. Convert File path to resource Key

I have ResourceDictionary in my application. I need to add some items from c# code to this collection:
<UserControl.Resources>
<ResourceDictionary>
</ResourceDictionary>
</UserControl.Resources>
As key for resources i want to use path to file.
For example:
c:\some folder\##file.txt
What is the best wey to convert this file path to valid ResourceDictionary Key?
<UserControl.Resources>
<ResourceDictionary xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String x:Key="c:some folder#file.txt">
whatever
</sys:String>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Label Content="{StaticResource c:some folder#file.txt}" />
</Grid>
Remove back slash and encode special characters.
This article on CP tell you how use loose XAML files at Runtime, also some other. Have a look.

Adding resources into Silverlight application

I am trying to add resource dictionary into my silverlight-4 aplication (suggested in "Applying a View to a ViewModel" chapter of the http://msdn.microsoft.com/en-us/magazine/dd419663.aspx article).
The 1st problem: I don't see any resource in my MainPage. Am I correctly understand that I need to add resource dictionary manually into Silverlight app?
The 2nd: When I did that, in the Dictionary1.xaml file
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib">
<DataTemplate DataType="{x:Type vm:MyViewModel}">
<vw:MyView />
</DataTemplate>
</ResourceDictionary>
I am getting an error: Can't resolve a symbol 'DataType'...
Is there any idea hot to do that?
ad 1: MainPage has a ResourceDictionary. You add elements to it in xaml like this:
<MainPage>
<MainPage.ResourceDictionary>
<DataTemplate>
<vw:MyView />
</DataTemplate>
</MainPage.ResourceDictionary>
...
You can add a ResourceDictionary to the MainPage.ResourceDictionary by using the Source and MergedDictionaries properties of ResourceDictionary:
<MainPage>
<MainPage.ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</MainPage.ResourceDictionary>
...
ad 2: DataTemplate does not have a Property DataType in the Silverlight framework. :-(
You also need to add an x:Key into the DataTemplate if it's going to be in the ResourceDictionary.

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

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.

Resources