我目前为这样的用户控件定义了背景:
<UserControl.Background>
<ImageBrush ImageSource="{DynamicResource LeftMenuBackgroundImage}" />
</UserControl.Background>
如何将其移至代码隐藏,例如:
伪码:
StackPanel sp = new StackPanel();
sp.Background = new ImageBrush(DynamicResource.GetResourceName("LeftMenuBackgroundImage"));
答案 0 :(得分: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>