我正在为按钮以自定义方式显示样式

想法是在Tag属性中编写Image源,在Content属性中编写Text。

所以风格就这样

<Style x:Key="NormalButtonStyle" TargetType="Button">
    <Setter Property="Background" Value="Black"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="Padding" Value="12,4,12,4"/>
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="FontWeight" Value="SemiBold"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid Background="{TemplateBinding Background}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Image Source="{TemplateBinding Tag}" Height="{TemplateBinding Height}" Width="{TemplateBinding Height}"/>
                    <ContentPresenter x:Name="ContentPresenter" Grid.Column="1" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Foreground" Value="Red" />
                    </Trigger>

                    <Trigger Property="IsPressed" Value="true">

                        <Setter Property="Foreground" Value="Gray"/>

                    </Trigger>

                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


图片不显示,仅显示文字



我在MainWindow中这样称呼按钮

<Button Style="{DynamicResource NormalButtonStyle}" Tag="/iPARK;component/Resources/Sign%20out_icon.png" HorizontalAlignment="Right" Margin="20" Click="Signout_Click"/>


如果我像这样直接在样式文件中设置图像,则会显示该图像,但不能再用于其他图像

<Image Source="/iPARK;component/Resources/Sign%20out_icon.png" Height="{TemplateBinding Height}" Width="{TemplateBinding Height}"/>

最佳答案

当您将图像URI放入Tag属性时,它不会自动转换为ImageSource(就像对Image.Source属性所做的那样)。您需要明确地做到这一点:

<Button ...>
    <Button.Tag>
        <BitmapImage UriSource="/iPARK;component/Resources/Sign%20out_icon.png"/>
    </Button.Tag>
</Button>

关于wpf - 新按钮样式的图片未显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15050316/

10-17 01:31