WPF ResourceDictionaries - wpf

I have a wpf tab control which is highly customized through styles. I have referenced those styles in a resourcedictionary "TabControlResources". How do I reference "TabControlResources" in another resourcedictionary?. I have a resourcedictionary called "MainViewResources" and I would like to reference the tab style from my "TabControlResources" in that resourcedictionary.
Thanks in advance.

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="pack://application:,,,/path/to/TabControlResources.xaml" />
</ResourceDictionary.MergedDictionaries>
<other stuff...>
</ResourceDictionary>

Related

How can I add a control template to a resource dictionary?

So I would like to create a template copy of a ComboBox, but not define it in the MainWindow, but in a specially created resource directory.
What i got:
<Application x:Class="dingeTesten.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:dingeTesten"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary Source="Dictionary1.xaml"/>
</Application.Resources>
And now i createt a RessourceDictionaray that got called Dictionary1.xaml. In this file i want the Template Copy of my ComboBox but when i create a template i cannot choose this file:
Its in German but i cannot choose the last option RessourceDictionary to save this template in my RessourceDictionary.
I suppose that you are trying to create a style for ComboBox in Blend. To enable Resource dictionary option you have to add ResourceDictionary into MergedDictionaries in App.xaml.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"/>
<ResourceDictionary Source="Dictionary2.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Then Resource dictionary option will be enabled.

How to use ModernUI with just a Usercontrol WPF

I wanting to you the styles from ModernUI with just a UserControl meaning i have no app.xaml file and i am unsure how to reference firstfloor.ModernUI in just to a usercontrol to inherit the styling.
You can add the styles as resources in your user control:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.xaml" />
<ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.Dark.xaml"/>
...

WPF how to force designer to display custom window style

I made a new CustomControl based on the Window Control.
When I use my Control it doesn't appear in the designer mode, instead it still uses the default window style.
How can I force the designer to display my window style instead of the default one?
My MainWindow.xaml:
<CustomWindow:MetroWindow x:Class="Testz.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:CustomWindow="clr-namespace:MetroWindow;assembly=MetroWindow"
Title="MainWindow" Height="350" Width="525" BorderBrush="Red">
<Grid>
</Grid>
</CustomWindow:MetroWindow>
Link to my whole project - maybe you'll need it
How it looks in the designer and how it really looks:
I think I understood what you was trying to accomplish.
The problem is that the Visual Studio Designer can't find the Resource because it is on the library. What you need to do is to create a ResourceDictionary pointing to it on you Application to be able to see the designer time template.
<Application x:Class="DemoMetroWindow.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MetroWindow"
StartupUri="DemoWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:/MetroWindow;component/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
You can learn more from links bellow.
OnApplyTemplate() never being called
WPF get Type in Design time?
http://blogs.msdn.com/b/jgalasyn/archive/2007/10/29/troubleshooting-wpf-designer-load-failures.aspx
http://blogs.msdn.com/b/jnak/archive/2007/11/08/code-behind-and-the-wpf-designer.aspx
You're using Mahapps Metro, right?
You can use the styles provided by it.
Styling a window with Metro
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
You can change the color of the window by changing the Resource dictionary of Blue.xaml by other colors, just check it out.
When the resource references in App.xaml are fine you should restart Visual Studio. In most cases the themes are then displayed correctly.
Regards

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?

WPF 4: MergedDictionaries don't seem to work any more

I have the below block of XAML
'BaseStyles.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ShinyBlue.xaml"/>
<ResourceDictionary Source="DataGrid.Generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Forms that reference this work in design-time, but not in run time.
If my form references ShinyBlue.xaml or DataGrid.Generic.xaml directly, that style sheet works.
EDIT
If I paste this directly into the form, it works correctly. Appearently the problem somehow has to do with my wrapper.
Broken
<Window.Resources>
<ResourceDictionary Source="../BaseStyles.xaml"/>
</Window.Resources>
Works
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ShinyBlue.xaml"/>
<ResourceDictionary Source="DataGrid.Generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
Try including your whole path from namespace to filename:
<ResourceDictionary Source="pack://application:,,,/[YourDll];component/[YourLocation]/ShinyBlue.xaml"/>
Where [YourDll] is the name of your project, and [YourLocation] is the location where the ResourceDictionary resides in your dll.

Categories

Resources