ResourceDictionary administration - wpf

The main WPF project has resources defined in App.xaml.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="pack://application:,,,/Views;component/Themes/Standard.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Here Views is another project which contains all kinds of UserControls.
This works all right, except that in the UserControls xaml the styles of Standard.xaml are not recognized because in this project App.xaml is not loaded.
So, compile time warnings like Resource cannot be resolved are shown.
It can be fixed by adding
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="pack://application:,,,/Views;component/Themes/Standard.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
to all UserControl files, but I would like to add this only one time somewhere in this project that doesn't contain App.xaml.
I can also ignore the warnings of course.
Inheritance for UserControl is when I understand correctly only possibe in a .cs file, not with a .xaml file.
Any other suggestions?

Related

How to use resource dictionaries in custom control library?

I've created a resource dictionary with some frequently used brushes.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="GrayColor1" Color="#f2f2f2"/>
<SolidColorBrush x:Key="GrayColor2" Color="#e5e5e5"/>
<SolidColorBrush x:Key="GrayColor3" Color="#d9d9d9"/>
...
</ResourceDictionary>
I want to use them in many controls in the custom control library, but I didn't find any way to make them available to the controls.
In a normal app I will put them in App.xaml, but in a library there is no App.xaml file.
So what is the way to use resource dictionary in a library?
I already tried without success to merge the dictionary into /Themes/Generic.xaml as this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyControls;component/DefaultBrushes.xaml"/>
<ResourceDictionary Source="/MyControls;component/Styles/CustButton.xaml"/>
<ResourceDictionary Source="/MyControls;component/Styles/CustTextBox.xaml"/>
...
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
But referencing the resources results in a null reference (seems that in Generic.xaml can be merged only control templates).
You have to merge them in each control, or in the most top control if you have nested controls.
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://Application:,,,/MyControls;component/Styles/CusTextBox.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
You can still create your merged dictionaries in your applications App.xaml, but in your controls library where you want to access those brushes, try using DynamicResource instead of StaticResource.
Background="{DynamicResource GrayColor1}"

How to include and use Themes in wpf application

I have to include themes to my wpf application and use them for look and feel of the windows(.xaml) and controls.
i tried to include the theme files like below code but it is throwing error.
<Application.Resources>
<ResourceDictionary Source="\Themes\BureauBlack.xaml"/>
How to assign source path of a theme file which is assign Build action as Resource
Try to use MergedDictionaries:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Generic_UI/BlueColors.xaml"/>
<ResourceDictionary Source="Generic_UI/KStyles.xaml"/>
<ResourceDictionary Source="Generic_UI/KButtons.xaml"/>
<ResourceDictionary Source="Generic_UI/KWindow.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

XAML ResourceDictionary same-assembly reference

I'm developing a WPF control library, and I need to reference a resource dictionary defined in the same assembly.
I managed to get it working with a separate-assembly reference.
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="pack://application:,,,/MyLocalAssembly;component/Foo.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
While with a same-assembly reference it doesn't work, and raises an exception ("Cannot locate resource 'Foo.xaml'.") at load time. Note that in the designer everything works fine, no matter which method I use.
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="pack://application:,,,/Foo.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Don't use absolute path for the resource dictionary. Simply use relative path reference.
For example,
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="folder/Foo.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>

Using Styles in merged ResourceDictonaries in Silverlight 5

I've got a bunch of styles in my app.xaml and they are all being used just fine in my pages within my SL5 app. I'd like to move these styles to multiple resource dictionaries to make it more manageable and consumable.
First I copied a style to a new resource dictionary in the /Styles/ButtonStyles.xaml page in my project... a snippet looks like this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="RegistrationsRolloverImage" TargetType="Button">
<Setter Property="Template">...</Setter>
</Style>
<Style x:Key="FinancialLedgerRolloverImage" TargetType="Button">
<Setter Property="Template">...</Setter>
</Style>
</ResourceDictionary>
Next I added the following to my App.xaml:
<ResourceDictionary x:Key="MergedStyles">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/ButtonStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
It forced me to add a x:key to the ResourceDictionary tag as I kept getting a build error. Now it builds, but the buttons that use the style aren't getting the style. In fact I'm getting a JS error that it can't find a style with the name of the two styles in my resource dictionary. They work just fine if they are in the App.xaml, but not if they are in seperate resource dictionary. I reflected the generated DLL and can see the styles/buttonstyles.xaml in the DLL.
At a loss... and can't figure out what's wrong. Ideas?
Are they within the same project? Try something more like this in your app.xaml;
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/YourResDictionaryContaining.Proj.Name;component/Styles/ButtonStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I have to do this to have consolidation of resourcedict's stored in one project, and then add that to the app.xaml of each other project to make them available globally. Currently I run about 6 Resource Dicts acros 20 projects in the same solution this way and it works great.
In the full App.xaml sample you want use "local" resources.
But when you have "local" resources and you want to merge a resource directory the systax a little bit different.
Try it like this:
<Application ...>
<Application.Resources>
<ResourceDictionary>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/ButtonStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="BaseTextBlock" TargetType="TextBlock">
...
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>

Referencing Themes in a dll in WPF

I am using an opensource wpf TreeListView control. The control is a library project with a themes folder in it containing some xaml theme files. In my wpf project, I have got a reference to the control dll and I would like to know how to reference the dll themes in app.xaml. I tried doing something like this but it throws exception saying can't find the treelistview assembly
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml" />
<ResourceDictionary Source="/TreeListView;component/themes/Aero.NormalColor.xaml" />
</ResourceDictionary>
</Application.Resources>
At the moment I have to copy all the theme files in my wpf project locally to make it work like this.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml" />
<ResourceDictionary Source="themes/aero.normalcolor.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Is there a way to reference theme files directly in the TreeListView dll like referencing default themes.
Awaiting
Nabeel
I figure it out myself, I was using the wrong assembly file name.

Resources