Access values from xaml file in the code(View) directly

Multi tool use
Access values from xaml file in the code(View) directly
I have a file (icons.xaml) that I added as a file in the Resources of my project. The file content is given below:
Icons.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<VisualBrush x:Key="Bucket"
Stretch="Uniform">
<VisualBrush.Visual>
<Viewbox Stretch="xxxx">
<Canvas Width="xxxxx"
Height="xxxx"> and so on...
Icons.xaml file's properties in vs 2017
Now, I want to consume the VisualBrush mentioned above, in my XAML (View) code directly. The lines are given below:
XAML Code (View)
<Setter Property="Icon">
<Setter.Value>
<Rectangle Fill="{Binding Bucket, Source={x:Static resx:Resources.Icons}}"/>
</Setter.Value>
</Setter>
The "resx" namespace is defined as:
xmlns:resx="clr-namespace:MyProjectNamespace.Properties"
When I run my project I cannot see my icons being binded to the rectangle's fill property and I am getting some xaml intellisense error which is "Cannot resolve property "Bucket" in the context of type "string".
Objective: I want to use the visualbrush key directly in the XAML in my Rectangle's Fill property in my View code.
Note: I have a restriction that I cannot use the code in the view which is given below, I cannot use <ResourceDictionary>
tab in the xaml code.
<ResourceDictionary>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyProjectNamespace;component/Resources/Icons.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Am I following the right approach regarding my objective or should I change this approach?
I have seen the methods that are used here in this answer but none of them are working Get values from *.resx files in XAML
icons.xaml
<Rectangle Fill="{StaticResource Bucket}"/>
Ok, first I need to make it a resx file then I should carry on?. Because, I have a restriction to use Mergedictionary tabs (or words) in my xaml file
– Newbie007
Jul 3 at 8:32
I thought you need access to VisualBrush, not .resx file. I suggested how to get it from ResourceDictionary. "have a restriction to use Mergedictionary tabs (or words) in my xaml file" - no idea what this could mean
– ASh
Jul 3 at 8:42
@ASh I have modified my question
– Newbie007
Jul 3 at 8:52
Since you have a restriction to use Merged dictionaries, can you use local resources?
– Lukáš Koten
Jul 3 at 9:07
2 Answers
2
I think the main issue here is that Binding is actually looking for a property called Binding on the Resources.Icons object.
Try this:
<Setter Property="Icon">
<Setter.Value>
<Rectangle Fill="{Binding Bucket, Source={x:Static resx:Icons}}"/>
</Setter.Value>
</Setter>
If I write your code in the xaml, I receive error on the on the "Icons" word, saying "cannot resolve symbol 'Icons'"
– Newbie007
Jul 3 at 8:36
Binding is not necessary, just try:
<Rectangle Fill="{x:Static resx:Resources.Icons.Bucket}"/>
<Rectangle Fill="{x:Static resx:Resources.Icons.Bucket}"/>
Compile error "Cannot find the member "Bucket" on the target type".
– Newbie007
Jul 3 at 8:56
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
icons.xaml
isn't a .resx file. what you need is StaticResource (or DynamicResource)<Rectangle Fill="{StaticResource Bucket}"/>
. but you have to load "icons" resource dictionary first into MergedDictionaries of your view in your xaml code– ASh
Jul 3 at 8:28