C# WPF按钮在样式中自动标注背景图像

C# WPF按钮在样式中自动标注背景图像,c#,wpf,xaml,C#,Wpf,Xaml,我想为WPF中的按钮创建一个样式,按钮的性质就像它显示一个背景图像,在鼠标上显示一个不同的背景图像。 我为此创建了下面的xaml结构 <Style x:Key="ShelfRefreshButton" TargetType="Button"> <Setter Property="OverridesDefaultStyle" Value="True"/> <Setter Property="Width" Value="

我想为WPF中的按钮创建一个样式,按钮的性质就像它显示一个背景图像,在鼠标上显示一个不同的背景图像。 我为此创建了下面的xaml结构

 <Style x:Key="ShelfRefreshButton" TargetType="Button">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Width" Value="22"/>
            <Setter Property="Height" Value="21"/>    
            <Setter Property="Background">
                <Setter.Value>
                    <ImageBrush ImageSource="images\refresh_active.png" />
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Name="border" 
                            BorderThickness="0" 
                            Background="{TemplateBinding Background}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" TargetName="border">
                                    <Setter.Value>
                                        <ImageBrush ImageSource="images\refresh_mouseover.png" />
                                    </Setter.Value>
                                </Setter>

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

我面临的问题是样式的宽度和高度。我们怎样才能到达汽车站?按钮应根据图像大小调整大小。因此,无需设置宽度和高度硬编码值。
在上面的xaml样式中,如果删除“宽度和高度属性”按钮,则根本看不见。如何解决这个问题?我需要在样式上应用哪些更改?

使用ImageBrush有点棘手,我建议您创建新的图像元素,并控制它。将图像和ContentPresenter放入网格。接下来,给图像命名,然后您可以控制它。之后,确保将Image设置为NoResize,并将按钮宽度和高度绑定到Image的实际宽度和实际高度