从资源中设置样式模板中的图像

时间:2015-03-26 13:06:58

标签: wpf image templates

我为窗口添加了自定义样式。但是我无法在标题栏中显示图像。

我尝试制作图像资源并将图像的静态资源设置为图像源,如本主题所示: WPF Setting Image.Source from DataTrigger(来自ExplodingRabbit的答案)

我想要的是在我的项目资源中有一个图像,并将此资源添加到标题栏中的图像源。 (在这里用图像说的代码!)

为简单起见,我只添加了风格的重要部分。

<Style x:Key="MainWindow" 
       TargetType="{x:Type Window}">
    <Setter Property="Foreground" 
            Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Window}">
                <Grid Background="{DynamicResource AchmeaGrey}">
                    ........................
                            <Border x:Name="PART_TITLEBAR">
                                <Grid>

                                    <IMAGE HERE!>

                                    <TextBlock />
                                    <DockPanel LastChildFill="False">
                                        <Button />
                                        <Button />
                                    </DockPanel>
                                </Grid>
                             </Border>
                    ........................
                </Grid>

...................

1 个答案:

答案 0 :(得分:2)

您可以在窗口资源中或在创建主窗口样式的任何位置创建ImageSource资源:

<ImageSource x:Key="MyImage">pack://application:,,,/Images/image.jpg</ImageSource>

ControlTemplate中,只需将图片上的Source属性设置为ImageSource密钥的属性。

<Image Source="{DynamicResource MyImage}" ... />