1

我目前为这样的用户控件定义背景:

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

如何将其移至代码隐藏,例如:

伪代码:

StackPanel sp = new StackPanel();
sp.Background = new ImageBrush(DynamicResource.GetResourceName("LeftMenuBackgroundImage"));
4

1 回答 1

2

请允许我回答这个问题,让它像这样工作:

在代码中:

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

在资源中:

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

<ImageSource x:Key="LeftMenuBackgroundImage">Images/LeftMenuBackground.jpg</ImageSource>
于 2009-07-27T12:42:54.553 回答