Failed to assign to property 'Windows.UI.Xaml.Controls.ContentControl.Content' - c#

In my windows 10 app I want to set button content from a resource file. In the designer the vector image from the resource file is visible. But in runtime the following exception occurs:
An exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in Demo.UI.W10.exe but was not handled in user code
WinRT information: Failed to assign to property 'Windows.UI.Xaml.Controls.ContentControl.Content'. [Line: 15 Position: 17]
Additional information: The text associated with this error code could not be found.
This is my resourche dictionary Icons.xaml wich holds the vector image in the Viewbox control with the key WiFi:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Viewbox x:Key="Wifi">
<Grid Width="48" Height="48" Visibility="Visible">
<Path Data="M16.104004,15.776998C17.199005,15.776998 18.086014,16.664998 18.086014,17.759999 18.086014,18.855977 17.199005,19.742999 16.104004,19.742999 15.009003,19.742999 14.12001,18.855977 14.12001,17.759999 14.12001,16.664998 15.009003,15.776998 16.104004,15.776998z M16.104004,10.558985C19.327011,10.558985,22.057007,12.679008,22.975006,15.601004L21.118011,16.756978C20.652008,14.412986 18.584015,12.646995 16.104004,12.646995 13.580002,12.646995 11.486008,14.474997 11.067001,16.87798L9.1930084,15.730001C10.070007,12.741997,12.831009,10.558985,16.104004,10.558985z M16.028015,5.2879915C21.153015,5.2879915,25.555008,8.378993,27.476013,12.796989L25.771011,13.859C24.221008,9.9980106 20.443008,7.2719988 16.028015,7.2719988 11.586014,7.2719988 7.7890015,10.031 6.2570038,13.929984L4.5440063,12.879997C6.4450073,8.4169874,10.871002,5.2879915,16.028015,5.2879915z M16.028015,0C23.047012,5.5224518E-08,29.114014,4.0700049,32,9.9789981L30.128006,11.144982C27.639008,5.8550076 22.262009,2.1920154 16.028015,2.1920151 9.7550049,2.1920154 4.3480072,5.9020047 1.881012,11.24801L0,10.094995C2.8630066,4.1239905,8.9640045,5.5224518E-08,16.028015,0z" Stretch="Uniform" Fill="#FF000000" Width="48" Height="48" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5">
</Path>
</Grid>
</Viewbox>
This is my App.xaml where I add the Icons.xaml resource file in the MergedDictionaries:
<Application
x:Class="Demo.UI.W10.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Demo.UI.W10"
RequestedTheme="Light">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Assets/Icons.xaml"/>
<ResourceDictionary Source="Assets/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
In my mvvmcross page I use the vector image WiFi from the resource file to set the button content.
<views:MvxWindowsPage
x:Class="Demo.UI.W10.Views.ConnectionView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Demo.UI.W10.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:Demo.Core.ViewModels"
xmlns:views="using:Cirrious.MvvmCross.WindowsUWP.Views"
mc:Ignorable="d">
<Page.Resources>
<vm:ConnectionViewModel x:Key="ViewModel"/>
</Page.Resources>
<Grid>
<Button Content="{StaticResource Wifi}" Command="{Binding DemoCommand, Source={StaticResource ViewModel}}" Style="{StaticResource BtnMenuStyle}"/>
</Grid>
How can I use the vector image from the resource dictionary as button content?

You can do placing the resources inside a DataTemplate:
<DataTemplate x:Key="Wifi">
<Viewbox >
<Grid Width="48" Height="48" Visibility="Visible">
<Path Data="M16.104004,15.776998C17.199005,15.776998 18.086014,16.664998 18.086014,17.759999 18.086014,18.855977 17.199005,19.742999 16.104004,19.742999 15.009003,19.742999 14.12001,18.855977 14.12001,17.759999 14.12001,16.664998 15.009003,15.776998 16.104004,15.776998z M16.104004,10.558985C19.327011,10.558985,22.057007,12.679008,22.975006,15.601004L21.118011,16.756978C20.652008,14.412986 18.584015,12.646995 16.104004,12.646995 13.580002,12.646995 11.486008,14.474997 11.067001,16.87798L9.1930084,15.730001C10.070007,12.741997,12.831009,10.558985,16.104004,10.558985z M16.028015,5.2879915C21.153015,5.2879915,25.555008,8.378993,27.476013,12.796989L25.771011,13.859C24.221008,9.9980106 20.443008,7.2719988 16.028015,7.2719988 11.586014,7.2719988 7.7890015,10.031 6.2570038,13.929984L4.5440063,12.879997C6.4450073,8.4169874,10.871002,5.2879915,16.028015,5.2879915z M16.028015,0C23.047012,5.5224518E-08,29.114014,4.0700049,32,9.9789981L30.128006,11.144982C27.639008,5.8550076 22.262009,2.1920154 16.028015,2.1920151 9.7550049,2.1920154 4.3480072,5.9020047 1.881012,11.24801L0,10.094995C2.8630066,4.1239905,8.9640045,5.5224518E-08,16.028015,0z" Stretch="Uniform" Fill="#FF000000" Width="48" Height="48" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5">
</Path>
</Grid>
</Viewbox>
</DataTemplate>
And now simply
<Button ContentTemplate="{StaticResource Wifi}"/>
You can use it as many times you need.

Related

WindowChrome/title bar theme not syncing with loaded PresentationFramework/XAML

I'm attempting to build a WPF app which uses the Royale theme from XP Media Center. I've correctly referenced the PresentationFramework.Royale DLL and the corresponding XAML resources into my App.xaml, and can confirm that the controls are very much Royale-themed:
However, the window itself fails to be, and the WindowChrome/title bar/window border theme fails to sync up, remaining the standard presentation style for Windows 10. This, of course, is what I'm trying to replicate, at least visually:
Relevant XAML, if useful (all *.cs files are default):
App.xaml:
<Application x:Class="ExampleRoyale.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Ariadne"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Royale;component/themes/Royale.NormalColor.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
MainWindow.xaml:
<Window x:Class="ExampleRoyale.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ExampleRoyale"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="94,39,0,0" VerticalAlignment="Top" Width="120"/>
<ComboBox HorizontalAlignment="Left" Margin="94,12,0,0" VerticalAlignment="Top" Width="120"/>
<Label Content="Label" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="79"/>
<Label Content="Label 2" HorizontalAlignment="Left" Margin="10,39,0,0" VerticalAlignment="Top" Width="79"/>
<RadioButton Content="RadioButton" HorizontalAlignment="Left" Margin="228,15,0,0" VerticalAlignment="Top" Width="102"/>
<RadioButton Content="RadioButton 2" HorizontalAlignment="Left" Margin="228,44,0,0" VerticalAlignment="Top" Width="102"/>
<TabControl Margin="82,72,82,71">
<TabItem Header="TabItem">
<Grid Background="#FFE5E5E5"/>
</TabItem>
<TabItem Header="TabItem">
<Grid Background="#FFE5E5E5"/>
</TabItem>
</TabControl>
</Grid>
</Window>
Is there a manner to apply the Royale theme to the window container itself, or is Royale controls the closest I will get?
Is there a manner to apply the Royale theme to the window container itself
Unfortunately (?) the window container itself is not part of WPF so there is no custom style for it. It's part of the operating system.
You may be able to make Windows 10 look like Vista by changing the OS settings as a user (I haven't tried) but this is not related to WPF.

How can I convert an `enum` to a XAML vector icon object and display it multiple times within the same window?

I have a resource dictionary in a Resources.xaml file containing multiple vector icons (XAML format, Canvas in a Viewbox):
<ResourceDictionary>
<Viewbox x:Key="Icon1" x:Shared="False">
...
</Viewbox>
<Viewbox x:Key="Icon2" x:Shared="False">
...
</Viewbox>
</ResourceDictionary>
These icons can be displayed in a WPF window multiple times because I have used the x:Shared="False setting. For example, ...
<ContentControl Content="{StaticResource Icon1}" />
<ContentControl Content="{StaticResource Icon1}" />
... displays the Icon1 icon twice as expected.
Now I'd like to convert an enum to the icon object so that an icon can be displayed based on an enum value (for nodes in a tree view). You would usually declare an EnumToObjectConverter in the Resources.xaml:
<local:EnumToObjectConverter x:Key="TreeIcons">
<ResourceDictionary>
<Viewbox x:Key="Icon1" x:Shared="False">
...
</Viewbox>
<Viewbox x:Key="Icon2" x:Shared="False">
...
</Viewbox>
<ResourceDictionary>
</local:EnumToObjectConverter>
But since this is an embedded resource dictionary the x:Shared setting does not have any effect (https://learn.microsoft.com/en-us/dotnet/framework/xaml-services/x-shared-attribute) and referencing the image through the converter results in the icon being displayed only once in the Window or tree view, even when referenced in multiple places (the other places remain blank).
How can I do a mapping from an enum to the vector icon object so that icons are still properly displayed in multiple places?
Update: This example demonstrates the effect of the x:Shared setting (this is a NET Core 3.0 WPF application in case it makes any difference).
MainWindow.xaml
<Window x:Class="XamlIconTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:XamlIconTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<StackPanel>
<Label Content="Icon1 (1st)" />
<ContentControl Content="{StaticResource Icon1}" Margin="8"/>
<Separator />
<Label Content="Icon1 (2nd)" />
<ContentControl Content="{StaticResource Icon1}" Margin="8"/>
<Separator />
<Label Content="Icon2 (1st)" />
<ContentControl Content="{StaticResource Icon2}" Margin="8"/>
<Separator />
<Label Content="Icon2 (2nd)" />
<ContentControl Content="{StaticResource Icon2}" Margin="8"/>
</StackPanel>
</Grid>
</Window>
MainWindow.xaml.cs
using System.Windows;
namespace XamlIconTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
Resources.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:XamlIconTest">
<!-- Icon1 without x:Shared -->
<Path x:Key="Icon1"
Width="37.9858" Height="46.6386" Canvas.Left="19.186" Canvas.Top="14.2229" Stretch="Fill" Fill="#FF000000" Data="F1 M 38.1789,60.8614L 19.186,37.7428L 38.1686,14.2229L 57.1718,37.7531L 38.1789,60.8614 Z "/>
<!-- Icon2 with x:Shared -->
<Path x:Key="Icon2" x:Shared="False"
Width="37.9858" Height="46.6386" Canvas.Left="19.186" Canvas.Top="14.2229" Stretch="Fill" Fill="#FF000000" Data="F1 M 38.1789,60.8614L 19.186,37.7428L 38.1686,14.2229L 57.1718,37.7531L 38.1789,60.8614 Z "/>
</ResourceDictionary>
Displayed main window (note the missing Icon1 in the first row):
Your question seems to boil down to two separate topics:
The primary one, which is how to share vector graphics in a context where x:Shared has no effect (i.e. in a resource dictionary that's defined as a child of your converter).
An implied secondary one, which is how to property select a specific vector graphic given an input value (e.g. an enum value).
First I will note: as a general rule it is my preference to use templates instead of x:Shared=false with explicit resources. It winds up doing basically the same thing — instantiating new visual objects for each value displayed — but IMHO is more idiomatic for WPF, which is designed entirely around the concept of templating and binding.
As far as addressing your issues goes…
Your MCVE does not involve code that uses a converter, but the basic principle would be the same, so I will provide an example based on the MCVE, not using a converter. The approach involves doing as I suggested in the comments, which is to declare a resource containing the path's data (i.e. the geometry), and then reuse that resource as needed. The data itself isn't a visual, and so can be shared arbitrarily.
First, the resource:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestSO58533019ShareVectorData"
xmlns:s="clr-namespace:System;assembly=mscorlib">
<PathGeometry x:Key="IconGeometry1">F1 M 38.1789,60.8614L 19.186,37.7428L 38.1686,14.2229L 57.1718,37.7531L 38.1789,60.8614 Z</PathGeometry>
</ResourceDictionary>
Then to use that, you can just define a DataTemplate that maps a Geometry object to the visual you want (in this case, a Path object):
<Window x:Class="TestSO58533019ShareVectorData.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestSO58533019ShareVectorData"
xmlns:s="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources.xaml" />
<ResourceDictionary>
<DataTemplate DataType="{x:Type Geometry}">
<Path Width="37.9858" Height="46.6386" Canvas.Left="19.186" Canvas.Top="14.2229"
Stretch="Fill" Fill="#FF000000"
Data="{Binding}"/>
</DataTemplate>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<StackPanel>
<Label Content="IconGeometry1 (1st)" />
<ContentControl Content="{StaticResource IconGeometry1}" Margin="8"/>
<Separator />
<Label Content="IconGeometry1 (2nd)" />
<ContentControl Content="{StaticResource IconGeometry1}" Margin="8"/>
</StackPanel>
</Grid>
</Window>
This results in the display of the icon twice:
Now, the above approach could still be used with your converter technique. Your converter could return different Geometry objects depending on the enum value, which in turn could be bound to the Data property of a Path object as above. With some contortions, you could even have a Path resource item that does this, using x:Shared=false to reuse that resource item.
But IMHO that would be harder than necessary and not the right way to go. To me, conceptually what is going on is that you have an enum value, and you want to represent that very value with some graphic, depending on the value. That's exactly what WPF's templating features are for! They map one data type to another (i.e. your enum type to a Path object), and with styles you can conditionally configure the templated object as needed.
For the sake of simplicity I will use int rather than an actual enum value. But the basic idea is exactly the same. Note that a key benefit of doing it this way is to minimize the amount of code-behind. You declare for WPF what it is you want to happen, instead of having to write procedural code to do something yourself that WPF could instead do for you.
First, let's define a couple of different icons:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestSO58533019ShareVectorData"
xmlns:s="clr-namespace:System;assembly=mscorlib">
<PathGeometry x:Key="IconGeometry1">F1 M 38.1789,60.8614L 19.186,37.7428L 38.1686,14.2229L 57.1718,37.7531L 38.1789,60.8614 Z</PathGeometry>
<PathGeometry x:Key="IconGeometry2">F1 M 38.1789,60.8614L 19.186,37.7428L 57.1718,37.7531L 38.1789,60.8614 Z</PathGeometry>
</ResourceDictionary>
Now, let's define a template for int, where that template uses style triggers to use the appropriate geometry data, and the bound value is simply that int value:
<Window x:Class="TestSO58533019ShareVectorData.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:p="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestSO58533019ShareVectorData"
xmlns:s="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources.xaml" />
<ResourceDictionary>
<DataTemplate DataType="{x:Type s:Int32}">
<Path Width="37.9858" Height="46.6386" Canvas.Left="19.186" Canvas.Top="14.2229"
Stretch="Fill" Fill="#FF000000">
<Path.Style>
<p:Style TargetType="Path">
<p:Style.Triggers>
<DataTrigger Binding="{Binding}" Value="1">
<DataTrigger.Setters>
<Setter Property="Data" Value="{StaticResource IconGeometry1}"/>
</DataTrigger.Setters>
</DataTrigger>
<DataTrigger Binding="{Binding}" Value="2">
<DataTrigger.Setters>
<Setter Property="Data" Value="{StaticResource IconGeometry2}"/>
</DataTrigger.Setters>
</DataTrigger>
</p:Style.Triggers>
</p:Style>
</Path.Style>
</Path>
</DataTemplate>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<StackPanel>
<Label Content="1st int" />
<ContentControl Margin="8">
<ContentControl.Content>
<s:Int32>1</s:Int32>
</ContentControl.Content>
</ContentControl>
<Separator />
<Label Content="2nd int" />
<ContentControl Margin="8">
<ContentControl.Content>
<s:Int32>2</s:Int32>
</ContentControl.Content>
</ContentControl>
</StackPanel>
</Grid>
</Window>
With that code, you'll get this:

Positioning controls - WPF

I am in the process of teaching myself WPF and have an issue that is confusing me. In the XAML below when I open the app the label and image are in the top left part of the window, but when I maximize the window they shift towards the middle. What am I missing that will make the controls keep their relative position?
<Window x:Class="WafLuckyDog.Presentation.Views.ShellWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:WafLuckyDog.Applications.ViewModels"
mc:Ignorable="d" Title="{Binding Title}" Icon="{StaticResource ApplicationIcon}" Width="994.273" Height="840"
d:DataContext="{d:DesignInstance vm:ShellViewModel}">
<Window.Background>
<ImageBrush ImageSource="{StaticResource Background}"></ImageBrush>
</Window.Background>
<DockPanel Margin="0,0,-539,0">
<Grid>
<Image Name="Logo" HorizontalAlignment="Left" Margin="0,10,0,669" Width="129"
Source="{StaticResource Logo}" />
<Label Content="Label" HorizontalAlignment="Left" Margin="10,145,0,0" VerticalAlignment="Top" Foreground="AntiqueWhite"/>
</Grid>
</DockPanel>
</Window>
First, you have a DockPanel that has no close tag and is doing nothing, it will give a compile error, so remove it. Also, remove the bottom margin and add a Height and VerticalAlignment properties. The VerticalAlignment and HorizontalAlignment ensures a top right corner anchor.
<Window x:Class="WafLuckyDog.Presentation.Views.ShellWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:WafLuckyDog.Applications.ViewModels"
mc:Ignorable="d" Title="{Binding Title}" Icon="{StaticResource ApplicationIcon}" Width="994.273" Height="840"
d:DataContext="{d:DesignInstance vm:ShellViewModel}">
<Window.Background>
<ImageBrush ImageSource="{StaticResource Background}"></ImageBrush>
</Window.Background>
<Grid>
<Image Name="Logo" HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top" Width="129" Height="129"
Source="{StaticResource Logo}" />
<Label Content="Label" HorizontalAlignment="Left" Margin="10,145,0,0" VerticalAlignment="Top" Foreground="AntiqueWhite"/>
</Grid>
</Window>

Get a Resource from a ResourceDictionary with various Resource Dictionary with a key

I need to have in a single file resources into a ResourceDictionary element with various resourceDictionary with key
The next code is a sample of this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/ presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<ResourceDictionary x:Key="Configuration">
<sys:String x:Key="_Conf_Delete_instr">Delete instrument</sys:String>
</ResourceDictionary>
<ResourceDictionary x:Key="Report">
<sys:String x:Key="mykey2">myvalue2</sys:String>
</ResourceDictionary>
</ResourceDictionary>
I wonder if when defining the content of an element can access the ResourceDictionary indicating the key:
<Button x:Name="btnDeleteInst" Content="{DynamicResource _Conf_Delete_instr}" HorizontalContentAlignment="Center" VerticalAlignment="Top" Margin="0,23,245,0" HorizontalAlignment="Right" Height="50" MinWidth="100" VerticalContentAlignment="Center" Click="btnDeleteInstr_Click"/>
The previous code throws an error on _Conf_Delete_instr because Key isnt found.
How I can access the resource _Conf_Delete_instr included in ResourceDictionary with key "Configuration"? With a converter?
Thanks.
Have an example:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Name="Configuration">
<sys:String x:Key="_Conf_Delete_instr">Delete instrument</sys:String>
</ResourceDictionary>
<ResourceDictionary x:Name="Report">
<sys:String x:Key="mykey2">myvalue2</sys:String>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
Test
<Button x:Name="btnDeleteInst" Content="{DynamicResource mykey2}" HorizontalContentAlignment="Center" VerticalAlignment="Top" Margin="0,23,245,0" HorizontalAlignment="Right" Height="50" MinWidth="100" VerticalContentAlignment="Center" />
<Button x:Name="btnDeleteInst1" Content="{DynamicResource _Conf_Delete_instr}" HorizontalContentAlignment="Center" VerticalAlignment="Top" Margin="0,23,245,0" HorizontalAlignment="Right" Height="50" MinWidth="100" VerticalContentAlignment="Center" />
Result

Import external Viewbox to UserControl

I have a UserControl where I want to add a Viewbox. The Viewbox is in an other xaml-file in my project. I tried a lot (something like ResourceDictionaries) ... but failed. Here is my minimal example:
UserControl:
<UserControl ......>
<Grid>
<!--Here I want the Viewbox (MyPicture.xaml)-->
</Grid>
</UserControl>
MyPicture.xaml
<Viewbox Width="16" Height="16" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Rectangle ... />
</Viewbox>
Hope anybody can help me.
As I can understand you need to re-use the view box, if so please try the next solution as a start point to your research.
Xaml code:
<Window x:Class="ResourceDictionaryProblemHelpAttempt.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<ContentControl Grid.Row="0" ContentTemplate="{StaticResource DataTemplateWithViewBox}"></ContentControl>
<ContentControl Grid.Row="1" ContentTemplate="{StaticResource DataTemplateWithViewBox}"></ContentControl>
<ContentControl Grid.Row="2" ContentTemplate="{StaticResource DataTemplateWithViewBox}"></ContentControl>
</Grid>
Resource dictionary code
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="DataTemplateWithViewBox">
<Viewbox Width="16" Height="16">
<Rectangle Width="16" Height="16" Fill="Tomato" Stroke="Black" StrokeThickness="1"></Rectangle>
</Viewbox>
</DataTemplate>
App.xaml code
<Application x:Class="ResourceDictionaryProblemHelpAttempt.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ViewPortContainingResourceDictionary.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Regards.
In the resource dictionary code, instead of using (code) , I want to just call the viewbox file. Especially since I have like 2000 lines of code and 30 different view box files. For example, it would be something like this:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="DataTemplateWithViewBox">
Get file information of viewbox1.xaml
</DataTemplate>
<DataTemplate x:Key="DataTemplateWithViewBox2">
Get file information of viewbox2.xaml
</DataTemplate>

Categories