views:

855

answers:

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"));
+1  A: 

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