Embed XAML file with ViewBox as a Resource - wpf

I have a XAML resource.
It looks like this.
How do I display this resource inside of a button?
Note:
I don't want to copy the code from the XAML image into App.xaml as a resource, I want to reference it properly from the Resources folder.
I would convert it to an SVG but I would rather use the image in its native format.

This example will guide you to reference a xaml resource in a proper way.
step 1
ResourceXaml (Example.xaml in a resource folder)
<Viewbox x:Key="A_Name">
// blahh...
</Viewbox>
app.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--Reference xaml file-->
<ResourceDictionary Source="/Resources/Example.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
step 2
Button in YourPage.xaml
<Button Content="{StaticResource Image_KeyName_In_Referenced_XAML_File}" />

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.

How can I access a dynamic resource from a different assembly inside a ViewBase

I am working with a Xaml file that is a custom view derived from a ViewBase, and I would like to access a DynamicResource that is in a different assembly. I have seen that it is possible to use something like:
<Application.Resources>
<ResourceDictionary
Source="/mylib;Resources/MyStyleDictionary.xaml" />
</Application.Resources>
However I'm dealing with a xaml file that looks something like:
<myLib:ViewBase>
<Grid>
<Button>
Style="{DynamicResource MyButtonStyle}" // I want this style to come from a different assembly
</Button>
</Grid>
</myLib:ViewBase>
How can I do this?
It's important to understand the difference between Dynamic and Static resources. What's the difference between StaticResource and DynamicResource in WPF?
But to answer the question:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/SomeOtherAssembly;Resources/SomeOtherDictionaryWithMyButtonStyleKey.xaml" />
<ResourceDictionary Source="/mylib;Resources/MyStyleDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
The resource is being referenced dynamically, so merging SomeOtherDictionaryWithMyButtonStyleKey.xaml before merging in MyStyleDictionary.xaml should work.

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.

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.

How to style a XAML window with a ResourceDictionary that exist in a DLL?

Hi I am trying to create a reusable XAML Window in a DLL.
I have placed in the Themes folder a new ResourceDictionary (I even merged it in the Generic.xaml), but when I try to use its styles in the window, I get an error message that the style doesn't exist:
<Window Style="{StaticResource ModalWindowStyle}" >
<!-- I have also the following -->
<Window.Resources>
<Style TargetType="Button" BasedOn="{StaticResource ButtonStyle}" />
</Window.Resources>
</Window>
I get an exception that this styles don't exist, they are both declared in that ResourceDictionary which is in the Themes folder.
From this post:
... as long as Project B has a reference to Project A.
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Project A;component/YourSubFolder/YourResourceFile.xaml" />
</ResourceDictionary.MergedDictionaries>
Then you can just use the Resources defined in YourResourceFile.xaml.

Resources