1

I currently define the background for a user control like this:

<UserControl.Background>
    <ImageBrush ImageSource="{DynamicResource LeftMenuBackgroundImage}" />
</UserControl.Background>

How can I move this to code-behind, e.g.:

PSEUDO-CODE:

StackPanel sp = new StackPanel();
sp.Background = new ImageBrush(DynamicResource.GetResourceName("LeftMenuBackgroundImage"));
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047

1 Answers1

2

allow me to answer this one, got it to work like this:

in code:

StackPanel sp = new StackPanel();
sp.SetResourceReference(StackPanel.BackgroundProperty, "LeftMenuBackgroundImageBrush");

in resources:

<ImageBrush x:Key="LeftMenuBackgroundImageBrush" 
    ImageSource="{DynamicResource LeftMenuBackgroundImage}"/>

<ImageSource x:Key="LeftMenuBackgroundImage">Images/LeftMenuBackground.jpg</ImageSource>
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047