使用WPF资源字典将图像放置在按钮中

时间:2019-04-03 17:21:50

标签: wpf

我正在尝试重新创建按钮模板,以便能够使其做出反应并以我希望的方式显示。我试图使图像成为按钮的主要美感,但是每当我从资源字典中将图像链接在一起时,都会引发错误。有什么建议吗?

资源字典代码:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:WPFTheme.Windows.Theme">





<Style x:Name="ButtonStyle" x:Key="ButtonStyle" TargetType="Button">

    <Style.Resources>
        <Image x:Key="Img" Source="{StaticResource Tag}"/>
    </Style.Resources>

    <Setter Property="BorderBrush" Value="#B7B7B7"/>
    <Setter Property="BorderThickness" Value="2"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Margin" Value="0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Margin="{TemplateBinding Margin}">
                    <Image Source="{StaticResource Img}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="BorderBrush" Value="White"></Setter>
        </Trigger>
    </Style.Triggers>

</Style>

窗口代码:

    <Window x:Class="WPFCMS.DesktopClient.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPFCMS.DesktopClient"
        mc:Ignorable="d"

        Title="Log In" Height="330" Width="300" FontSize="14" Background="#04c582"
        Icon="Images/Icon.ico">



    <Border Background="#2e3137" CornerRadius="20" Margin="20">

        <StackPanel Margin="20">

            <Label Content="Login" Foreground="White" FontSize="25" HorizontalAlignment="Center"/>
            <Separator/>

            <!--Username-->
            <Label Content="Username" Foreground="White"/>
            <TextBox Name="UsernameTextbox" Background="#545d5a" Foreground="White" FontSize="18"/>

            <!--Password-->
            <Label Content="Password" Foreground="White"/>
            <PasswordBox Name="PasswordTextbox" Background="#545d5a" Foreground="White" FontSize="18"/>

            <!--Buttons for Window-->
            <Border Padding="5">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <Button Style="{StaticResource ButtonStyle}" Tag="Images/Login.png" Height="50" Width="50" Background="Transparent">

                    </Button>



                </Grid>
            </Border>
        </StackPanel>

    </Border>
</Window>

错误:

Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll
Additional information: 'Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.' Line number '12' and line position '14'.

作为参考,该图像确实存在,并设置为“资源”。

0 个答案:

没有答案