基于主题在WPF/XAML中显示不同的图像

基于主题在WPF/XAML中显示不同的图像,wpf,xaml,Wpf,Xaml,我的问题似乎很简单:我的主页上有一个viewbox,根据选择的主题(theme1或theme2),我希望显示适当的图像(WhiteImage或BlackImage,在参考资料下的myImages.xaml中都定义为SVG画布) 这就是我想我能做的: <Viewbox x:Name="myCustomImage" VerticalAlignment="Top" Height="24" Margin="0,0,10,0" Child="{StaticResource myImage}" /&g

我的问题似乎很简单:我的主页上有一个viewbox,根据选择的主题(theme1或theme2),我希望显示适当的图像(WhiteImage或BlackImage,在参考资料下的myImages.xaml中都定义为SVG画布)

这就是我想我能做的:

<Viewbox x:Name="myCustomImage"
VerticalAlignment="Top"
Height="24"
Margin="0,0,10,0"
Child="{StaticResource myImage}" />

现在,我在mainpage.xaml Child=“{StaticResource WhiteImage}”/>中的这一行出现了一个不同的错误-在System.Windows.StaticResourceExtension上提供值引发了一个异常-找不到名为WhiteImage的资源。顺便说一句,当我将xaml更改为myImage时,我也会遇到同样的错误。谢谢您的帮助。与此相关的一点是,我发现很难相信它如此复杂,我所要做的就是:如果主题=黑色viewbox.child=白色图像,否则viewbox.child=黑色图像。

您正在使用字符串作为画布的子对象。你需要像这样声明孩子

<Canvas x:Key="myImage">
   <ContentControl Content="{StaticResource WhiteImage}" />
</Canvas>

您正在使用字符串作为画布的子对象。你需要像这样声明孩子

<Canvas x:Key="myImage">
   <ContentControl Content="{StaticResource WhiteImage}" />
</Canvas>

<Viewbox x:Name="myViewbox"
...
Height="32"
Child="{StaticResource WhiteImage}" />
public Canvas myImage
{
    get
    {
        return (Canvas)GetValue(myImageProperty);
    }
    set
    {
        SetValue(myImageProperty, value);
    }
}
<Canvas x:Key="myImage">
   <ContentControl Content="{StaticResource WhiteImage}" />
</Canvas>