0

I have a user control in which I want to set the background image. If I do that, it works:

The view:

<UserControl.Background>
        <ImageBrush AlignmentX="Center" AlignmentY="Center" Stretch="Uniform" Opacity="0.25" ImageSource="{Binding Logo}">
            <ImageBrush.RelativeTransform>
                <ScaleTransform ScaleX="0.75" ScaleY="0.75" CenterX=".5" CenterY="0.5" />
            </ImageBrush.RelativeTransform>
        </ImageBrush>
    </UserControl.Background>

The property in the view model:

private byte[] _logo = GlobalVariables.Logo;
        public byte[] Logo
        {
            get { return _logo; }
            set
            {
                _logo = value;
                base.RaisePropertyChangedEvent("Logo");
            }
        }

Really I load the logo in a global variable when I start the application, because the logo is stored in the database and in this way I only have to load once, not each time I want to print a document.

So it is a global variable, I am trying to get the logo directly from the global variables, so I can avoid the needed to have a property in my view model.

So in my view I am trying this code:

<UserControl.Background>
        <ImageBrush AlignmentX="Center" AlignmentY="Center" Stretch="Uniform" Opacity="0.25" ImageSource="{x:Static vg:GlobalVariables.Logo}">
            <ImageBrush.RelativeTransform>
                <ScaleTransform ScaleX="0.75" ScaleY="0.75" CenterX=".5" CenterY="0.5" />
            </ImageBrush.RelativeTransform>
        </ImageBrush>
    </UserControl.Background>

In this case, I get the following error:

'System.Byte[]' no es un valor válido para la propiedad 'ImageSource'.

I don't understand why, because in the example that works, the property in my view model is also a Byte[], in fact, in my view model I am setting the glbal variable to the property.

Thanks.

Álvaro García
  • 18,114
  • 30
  • 102
  • 193

1 Answers1

2

Use a static property Binding to benefit from built-in type conversion (which is apparently not utilized by StaticExtension):

<ImageBrush ImageSource="{Binding Path=(vg:GlobalVariables.Logo)}" .../>

Note that Logo is supposed to be a public static property here.

If it is a static field, you can write the Binding like this:

<ImageBrush ImageSource="{Binding Source={x:Static vg:GlobalVariables.Logo}}" .../>
Clemens
  • 123,504
  • 12
  • 155
  • 268