wpf - 在按钮的内容演示器中显示图像

标签 wpf button contentpresenter

我有一个按钮,其样式可以在其中显示图像。我希望能够使用 Content 指定它使用的图像按钮上的属性(或其他方式)。

如何在不实际在按钮中直接嵌套图像的情况下完成此操作。

<BitmapImage x:Key="closeImage" UriSource="close.png" />

我想我可以像这样指定图像文件名:
<Button Content="{{StaticResource closeImage}" x:Name="closeButton" Click="closeButton_Click" Style="{DynamicResource WindowToolboxButton}"/>

风格:
    <Style x:Key="WindowToolboxButton" TargetType="{x:Type Button}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
        <Setter Property="Background" Value="{StaticResource ButtonNormalBackgroundFill}"/>
        <Setter Property="BorderBrush" Value="{StaticResource ButtonBorder}"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid x:Name="grid" Height="15" Width="15">
                        <Border x:Name="border" CornerRadius="2,2,2,2" BorderBrush="#FFBBCDD2" BorderThickness="1" Opacity="0" Margin="0">
                            <Border.Background>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="#FF82A3AC" Offset="1"/>
                                    <GradientStop Color="#7FCDD9DC"/>
                                </LinearGradientBrush>
                            </Border.Background>
                        </Border>
                        <Image x:Name="image" Source="close.png" Stretch="None" VerticalAlignment="Center" HorizontalAlignment="Center" >

                        </Image>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

最佳答案

我很确定我错过了您的问题,但以下代码对我有用:

<Window.Resources>
 <Image x:Key="test" Source="/Images/ChangeCase.png"/>
</Window.Resources>

<!-- Using a Resource for the Content -->
<Button Width="100" Height="20" Content="{StaticResource test}"/>

<!-- Specifying the Content directly -->
<Button Width="100" Height="20">
 <Image Source="/Images/ChangeCase.png"/>
</Button>

另外我在你的代码中发现了一个错误:
<Button Content="{{StaticResource closeImage}" [...]

应该:
<Button Content="{StaticResource closeImage}" [...]

我不太明白你的风格在做什么。当您想通过样式指定图像时,为什么要将 Content 属性设置为 StaticResource?

编辑:

好的,我尝试了你的风格,它也适用于我。
   <Style x:Key="WindowToolboxButton" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid x:Name="grid" Height="15" Width="15">
                        <Border x:Name="border" CornerRadius="2,2,2,2" BorderBrush="#FFBBCDD2" BorderThickness="1" Opacity="0" Margin="0">
                            <Border.Background>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="#FF82A3AC" Offset="1"/>
                                    <GradientStop Color="#7FCDD9DC"/>
                                </LinearGradientBrush>
                            </Border.Background>
                        </Border>
                        <Image x:Name="image" Source="/Images/ChangeCase.png" Stretch="None" VerticalAlignment="Center" HorizontalAlignment="Center" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

  <Button Width="100" Height="20" Content="Test" Style="{DynamicResource WindowToolboxButton}"/>

图像显示。但是,由于 opacity-prop 设置为 0,我看不到您的边框。

直接在按钮上给出的内容被样式覆盖。

也许你的形象有问题?您是否将其构建属性设置为 资源 ?

关于wpf - 在按钮的内容演示器中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3503616/

相关文章:

wpf - 在 WPF 中,我可以在 ListView 中显示空行而不绑定(bind)任何东西吗?

c# - 当在表单上的某处单击按钮时,wpf 编辑列表框中的项目

jquery - 从嵌入式按钮单击获取选定的项目

Silverlight:ComboBox.ItemTemplate 中的 ContentPresenter 在单击 ComboBox 时会导致所有内容崩溃

wpf - ContentPresenter.ContentSource与内容

wpf - 在模板中,当 Path 是 ContentPresenter 时,我可以触发更改 Path.Fill 吗?

c# - 如何在 WPF 中保存控件的不模糊的高质量图像?

wpf - 将命令附加到 ListView 的 ScrollViewer.ScrollChanged

wpf - 如何让网格为空白单元格绘制边框?

windows-phone-7 - 如何阻止 Windows Phone 中的按钮控件在单击时发出丑陋的白色闪烁?