0

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.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

Newbie007
  • 55
  • 6

2 Answers2

-1

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>
Damjan
  • 49
  • 1
  • 7
  • If I write your code in the xaml, I receive error on the on the "Icons" word, saying "cannot resolve symbol 'Icons'" – Newbie007 Jul 03 '18 at 08:36
-1

Binding is not necessary, just try:

<Rectangle Fill="{x:Static resx:Resources.Icons.Bucket}"/>

Lukáš Koten
  • 1,191
  • 10
  • 22