ResourceDictionary制作图像样式

时间:2014-10-26 10:28:50

标签: wpf image resourcedictionary

我正在尝试制作一种在wpf中添加图像的样式。 这是我现在的代码。我在Visual Studio的设计模式中看到了图片但是当我启动程序时,图片不会显示出来。

我创建了一个ResourceDictionary,然后将它们添加到其中。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style TargetType="Image" x:Key="InstanceLogo">
    <Setter Property="Source">
        <Setter.Value>
            <BitmapImage UriSource="Resources/InstanceResources/logo.png"/>
        </Setter.Value>
    </Setter>
</Style>

<Style TargetType="Image" x:Key="AddNewInstance">
    <Setter Property="Source">
        <Setter.Value>
            <BitmapImage UriSource="Resources/ProgramResources/Add-New.png"/>
        </Setter.Value>
    </Setter>
</Style>
<Style TargetType="Image" x:Key="Settings">
    <Setter Property="Source">
        <Setter.Value>
            <BitmapImage UriSource="Resources/ProgramResources/Settings.png"/>
        </Setter.Value>
    </Setter>
</Style>
<Style TargetType="Image" x:Key="About">
    <Setter Property="Stretch" Value="Fill"/>
    <Setter Property="Source">
        <Setter.Value>
            <BitmapImage UriSource="Resources/ProgramResources/About.png"/>
        </Setter.Value>
    </Setter>
</Style>
</ResourceDictionary>

这是我尝试添加它们的窗口。

<Window x:Class="GhostLauncher.Client.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="GhostLauncher" Height="700" Width="800" WindowStyle="ThreeDBorderWindow">

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../ResourceDictionaries/ResourceDictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

<DockPanel>
    <ToolBarTray DockPanel.Dock="Top">
        <ToolBar>
            <Button>
                <Image Style="{StaticResource AddNewInstance}"  />
            </Button>
            <Button>
                <Image Style="{StaticResource Settings}"  />
            </Button>
            <Button>
                <Image Style="{StaticResource About}"  />
            </Button>
        </ToolBar>
    </ToolBarTray>
    <ListView DockPanel.Dock="Left">
        <ListViewItem>
            <StackPanel>
                <Image Style="{StaticResource InstanceLogo}" />
                <Label HorizontalAlignment="Center">Minecraft 1</Label>
            </StackPanel>
        </ListViewItem>
        <ListViewItem>
            <StackPanel>
                <Image Style="{StaticResource InstanceLogo}" />
                <Label HorizontalAlignment="Center">Minecraft 2</Label>
            </StackPanel>
        </ListViewItem>
        <ListViewItem>
            <StackPanel>
                <Image Style="{StaticResource InstanceLogo}" />
                <Label HorizontalAlignment="Center">Minecraft 3</Label>
            </StackPanel>
        </ListViewItem>
        <ListViewItem>
            <StackPanel>
                <Image Style="{StaticResource InstanceLogo}" />
                <Label HorizontalAlignment="Center">Minecraft 4</Label>
            </StackPanel>
        </ListViewItem>
        <ListViewItem>
            <StackPanel>
                <Image Style="{StaticResource InstanceLogo}" />
                <Label HorizontalAlignment="Center">Minecraft 5</Label>
            </StackPanel>
        </ListViewItem>
    </ListView>
    <StackPanel DockPanel.Dock="Right">

    </StackPanel>
</DockPanel>
</Window>

1 个答案:

答案 0 :(得分:1)

你需要的是:

<Style TargetType="Image" x:Key="InstanceLogo">
    <Setter Property="Source" Value="Resources/InstanceResources/logo.png">
</Style>