We are creating an office ribbon that opens up a WPF window that is stored in another WPF Control library project.
That WPF window has some themes attached to it that is stored in a ResourceDictionary that is compiled in a separate project.
However when we load up the WPF window, all the themes from the ResourceDictionary are lost.
We can fix this by manually/forcing the theme on the window itself, but this seems like a bad solution. So my question is: how can I load the theme of the new WPF window from the Office Addin application?
Uri uri = new Uri("/Nov.Presentation.RigDoc.WpfResources;component/Shared.xaml", UriKind.Relative);
Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);
I just tried this with Office 2010 (actually using a 2007 VSTO Addin but running it in 2010) and it works perfectly.
I´ve got an external project´s library referenced in the VSTO project and I use this following xaml in the control to link in the resource dictionary.
<UserControl.Resources>
<ResourceDictionary>
<!-- Link in th general styles -->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyAssemblyName;component/MyResourceDictionaryName.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Other style... -->
</ResourceDictionary>
</UserControl.Resources>
Otherwise I could think of it being a problem with your styles being overrided by some later explicit or implicitly linked in styles. If it cannot find the assembly you reference it should throw an example so the problem shouldn't be therein.
Related
Regarding this question and Marc's answer, I find this solution perfect, but I have trouble organizing my solution to make it functional.
How can I create a styling project that contains only XAML and reference to sub XAML, and how can I use it elsewhere in my solution? What is the visual studio project used by Marc in his answer to create the styling project?
Thank you,
B
You could create a WPF User Control Library in Visual Studio and add ResourceDictionary items where you define your XAML resources to it.
You then add a reference to this WPF User Control Library from your WPF Application project (Project->Add Reference in Visual Studio) and merge the resource dictionaries that are defined in the library in the App.xaml of your application:
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/WpfUserControlLibrary1;component/Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Replace "WpfUserControlLibrary1" with the name of the WPF User Control Library and "Dictionary1" with the name of the ResourceDictionary that you added to this project.
I'm new to WPF and struggling to use styles that live in a separate assembly. This is what I'm doing:-
I have a class library project with a \Themes folder containing a "generic.xaml" that merges a number of xaml files from a subfolder within \Themes:-
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Metro\CoreStyles.xaml" />
... etc ...
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
My solution also has a WPF application project, and in here the App.xaml merges in the resources from my library project like so:-
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyThemeLibrary;component/Themes/generic.xaml"/>
... etc...
Standard stuff so far.
Finally, I have a third project - a WPF user control library. These controls use these common styles, typically with "Style={StaticResource SomeStyle}". I can run the app and it all looks fine, but the problem is I don't get design-time support when writing the user controls - the design surface is basically empty.
Another SO article suggested adding an App.xaml to the user control library project, and merging in the resources as above. This works and I get my design-time support, however I get an error when trying to build the solution:
Library project file cannot specify ApplicationDefinition element.
I have tried changing the App.xaml build action from "ApplicationDefinition" to "Page", as has been suggested elsewhere. This gets the build working but I lose the design-time support as the user controls can no longer see the styles.
Is there a way around this problem, or failing this, an alternative way of using styles from another assembly?
Thanks in advance
Andrew
The error message says it. You cannot use "pack://application..." syntax in a library project. You should do this in your wpf project.
I have two projects in my solution. The first project is a WPF application, the other is a regular DLL project. Inside the DLL project I have some WPF user controls. I want these controls to share some resources and define them in the DLL.
I know that in a regular WPF application, you can specify application resources in App.xaml. Is there an equivalent in a DLL project?
Yes, you can create a resource XAML in the DLL like this (keep sure you have all WPF assemblies referenced in the DLL):
<!-- name of the dictionary is MyResources in MyDLL namespace -->
<ResourceDictionary x:Class="MyDLL.MyResources"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="./Controls/ButtonStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
and add this to the resources of your App class in your WPF project:
public App()
{
MyDLL.MyResources externalRes = new MyDLL.MyResources();
this.Resources.Add("MyExternalResources", externalRes);
}
No, there isn't an equivalent in a dll, because the resource loading isn't part of an assembly (exe), but part of an application. In order to load resources an application must be loaded. App is the root element of an application, rather than an exe assembly. To do the equivalent for controls in a dll you can create a separate ResourceDictionary and add it to each control's XAML by merging it into the UserControl's resources using ResourceDictionary.MergedDictionaries.
I am recieving following error in VS 2010.
I have two Silverlight projects. one project only contains styles and other project is my application. I have referenced styles project in my application and user static resources from that project in my whole application, but I keep recieving this error in VS though everything works fine when I compile and run the application.
The resource "SearchBoxStyle" could not be resolved
Yes, I stumped on this one today and I am also looking for a solution.
It seems to be a missing feature of Visual Studio - VS XAML designer just cannot find static resources outside of the current XAML file.
It seems, Expression Blend is smarter and offers a workaround for such cases.
Here is a similar thread which suggests using Blend:
The resource could not be resolved (VS 2010 RC)
And here is what Microsoft says about this issue:
http://msdn.microsoft.com/en-us/library/bb546934(VS.90).aspx#ResourcesatDesignTime
And for me the solution was to put a / in the path of my resource and set the xaml file to compile as a resource. So my App.xaml looks like this:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/ControlStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Dummy Style for WPF 4 bug fix, anything you won't use goes -->
<Style TargetType="{x:Type WebBrowser}" />
</ResourceDictionary>
</Application.Resources>
It failed to load at design time when I did not use the first slash in the path, but now it works fine at design time in other XAML pages.
we are using Infragistics WPF controls (e.g. xamDataGrid, xamDockManager etc), and we will be using the Infragistics Office 2007 Blue theme which these controls support.
We also want to style the rest of the application (i.e. standard WPF controls) using the same Office 2007 Blue style.
What's the best approach? Are there Office 2007 themes/skins that we can download or purchase? Can we use anything from the Infragistics download?
I know this is an old question, but maybe little update can be helpful for anyone that stumbles upon this. It is working in current version of Infragistics. Resource in App.xaml in enough. Sources point to folder in solution that contains files from Infragistics themes (usually something like C:\Program Files (x86)\Infragistics\2015.1\WPF\Themes).
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Theme/Styles.Shared.xaml" />
<ResourceDictionary Source="/Theme/Styles.WPF.xaml" />
<ResourceDictionary Source="/Theme/Theme.Colors.xaml" />
<ResourceDictionary Source="/Theme/IG.MSControls.Core.Implicit.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
IIRC for winforms infragistics allow you to use the app stylist to be able to theme the standard controls.
Just had a quick look on the forums and it doesn't look like it can be done for WPF.
How to apply themes to non-Infragistics controls