WPF binding to external style - wpf

I have found a framework called VIBlend http://viblend.com/products/net/wpf/controls/free-wpf-controls.aspx. I have downloaded it and there is a dll file containing styles. It contains a dll which contains styles. One of these Styles is contained in a file called ListBox.xaml (compiled in the dll).
It contains a few styles. One of them is declared like this:
Now i am wondering how I can use this style. If created a reference to that dll but now?
I tried this one but it does not work...
<UserControl.Resources>
<ResourceDictionary x:Key="listBoxStyle" Source="Office2010SilverListBoxStyle"/>
</UserControl.Resources>
<ListBox x:Name="listBox" Style="{StaticResource listBoxStyle}">

Try this:
<Application.Resources>
<ResourceDictionary
Source="/PutDllNameHere;component/ListBox.xaml" />
</Application.Resources>

Related

How to make a globally available style WITHOUT App.xaml

I have a class library I am creating for an AutoCAD plugin that includes WPF control elements. I would like to add a theme to these elements, so I added the WPFThemes.DarkBlend package from Nuget.
To property utilize the theme, you are supposed to add the style reference to your App.xaml file like so:
<Application
...
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes\Styles.xaml” />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Now unfortunately, because this is only a class library, I do not have an App.xaml file in which to add this code. I am able to use my Window.Resources, but in doing so, I am afraid that the styles in the reference are not exposed to the Window element. Is this because the Window is not within the scope of its own resources? Below is an image that shows my current situation:
Thank you!
After creating an x:Key in the style called "DarkWindow", I found that I was able to apply the style directly in the PluginWindow.xaml window properties like this:
<Window Style="{DynamicResource DarkWindow}" />

Use Resource Dictionary Styles in my class library project without merging dictionaries

I am creating a class library project that will contain WPF user controls. My Requirement is that all controls have the same style. My project looks like:
Things I have done in order to solve this problem:
Added all references needed by a WPF application System.Xaml, WindowsBase, etc.. so that I can have wpf controls in my class library project.
In AssemblyInfo.cs I have added:
[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]
Added ResourceDictionary1.xaml To the project adding the style.
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="Brush1" Color="#FF19199E"/>
</ResourceDictionary>
Now if I want to use a style on my UserControl1.xaml I do:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ResourceDictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid >
<Rectangle Fill="{StaticResource Brush1}" />
</Grid>
I know it works great but here is the catch. Every time I create a new userControl I will have to merge the dictionary. On a regular WPF application I could just merge the dictionary once on App.xaml and I will be able to use that dictionary on my entire application. How can I avoid having to merge the dictionary every time I create a new userControl? If I plan on addying a new resource dictionary I will have to go to all userControls and merge another dictionary. Perhaps I wrote the question title incorrectly and my question should have been how can I add a App.xaml file to a class library project
You should replace the source value ResourceDictionary1.xaml like the follow:
Source="pack://application:,,,/ControlsDLL;component/ResourceDictionary1.xaml">
or just simple as following:
<UserControl.Resources>
<ResourceDictionary Source="pack://application:,,,/ControlsDLL;component/ResourceDictionary1.xaml"></ResourceDictionary>
</UserControl.Resources>

Global style in a separate assembly

For my application themes, I created a separate class library MyApp.Themes.dll.
In the root folder of this library I have Standard.xaml:
<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:,,,/MyApp.Themes;component/Standard/Accordion.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
In Standard folder I have Accordion.xaml:
<ResourceDictionary xmlns:layoutPrimitivesToolkit="..."
xmlns:layoutToolkit="..."
...>
<!-- layoutPrimitivesToolkit:AccordionButton -->
<Style TargetType="layoutPrimitivesToolkit:AccordionButton">
...
</Style>
...
</ResourceDictionary>
which are default styles for WPF Toolkit Accordion control, except that style for
AccordionItem is modified to set background to transparent (instead of blue, which is a known bug).
For both xaml files Build Action is set to "Resource".
Now, after referencing MyApp.Themes library in MyApp WPF project, in App.xaml I wrote the following:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="pack://application:,,,/MyApp.Themes;component/Standard.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
It doesn't work (no error; just style not applied). If I copy&paste Accordion.xaml styles directly in App.xaml, then it works.
Some people suggested adding dummy resource dictionary, so I added the following in App.xaml after MergedDictionaries ending tag:
<Style TargetType="{x:Type layoutToolkit:AccordionItem}" />
Again, it doesn't work. Can someone help me solve this? By the way, I didn't try an approach
suggested by some others - using basedOn. If I must do that, I'd like to do in MyApp.Themes,
because it makes using themes nicer in App.xaml. Thank you in advance.
UPDATE1: It works if I directly copy&paste Accordion.xaml content to Standard.xaml
UPDATE2: I tried local assembly resource file URI (http://msdn.microsoft.com/en-us/library/aa970069.aspx) in Standard.xaml:
<ResourceDictionary Source="pack://application:,,,/Standard/Accordion.xaml"/>
It throws an error, saying standard/accordion.xaml not found.
It seems there's a bug in .NET. See the thread below:
Trouble referencing a Resource Dictionary that contains a Merged Dictionary
Also, there's a connect page on Microsoft site:
https://connect.microsoft.com/VisualStudio/feedback/details/609601/merge-dictionaries-does-not-work-when-we-merge-merged-dictionaries#tabs

How can i make a prefix so i can address a folder in xaml

I always have problems making new prefixes in xaml. Most of the time, i get the message that the URI cannot be found in the assembly. My setup:
I have a WPF project (in a solution with class libs and asp.NET projects) with a MainWindow.xaml file. The XAML starts with : Window x:Class="MainWindow" ... .
So as default, there's no namespace given to it. In that same project i made a folder "Folder". In that folder, i have resx-files. What i need to do is make a prefix in xaml so i can address those files. I was thinking of :
xmlns:p="clr-namespace:WpfApplication.Folder"
and then for my controls
<Label Content="{x:Static p:NameResxFile.KeyName></Label>
However, the prefix generates the "URI cannot be found in the assembly" error. I'm i just failing at making prefixes?
Thanks in advance.
EDIT
If you cannot make a namespace ref to a folder, what is happening here?
xmlns specifies namespaces, it does not bother with folders or files, if you need access to an external resource you can load it into your control's resources via ResourceDictionary.
There was something like this i think:
<Window.Resources>
<ResourceDictionary x:Key="ExternalRes" Source="Folder/File.xaml"/>
....
</Window.Resources>
To reference an element of resource dictionary you should add that dictionary to your control's Resources collection or register it in the App.xaml file. After that you could just use StaticResource extension to get access to the element. Your code will look like this:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Folder/NameResxFile.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<!-- ... -->
<Label Content="{StaticResource KeyName}"/>
Things get trickier if you want to put the resource dictionary to another assembly and reference it. For this purpose refer to PackURIs in WPF article.

Load WPF styles or other Static Resources from an external file or assembly

I have a few WPF applications and I want all my styles to be in a shared assembly instead of declaring them in each application separately.
I am looking for a way so I don't have to change all my Style="{StaticResource BlahBlah}" in the existing applications; I just want to add the reference to this style assembly, and delete it from the current application, so it's taken from the assembly.
Is there any way?
Referencing an external ResourceDictionary (XAML File):
<Application.Resources>
<ResourceDictionary Source="MyResources.xaml" />
</Application.Resources>
Referencing an external ResourceDictionary (DLL):
<Application.Resources>
<ResourceDictionary Source="/MyExternalAssembly;component/MyResources.xaml" />
</Application.Resources>

Resources