【问题标题】:WPF Button Automatic Dimension in Style with background image带有背景图像的 WPF 按钮样式中的自动尺寸
【发布时间】:2013-07-31 12:04:51
【问题描述】:

我想在 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>

我面临的问题是 Style 中的 Width 和 Height 。我们怎样才能让它成为汽车?该按钮应根据图像大小调整大小。因此无需设置 Width 和 Height 硬编码值。 在上面的 xaml 样式中,如果我删除宽度和高度属性按钮,我根本看不到鼻涕。如何解决这个问题?我需要对样式进行哪些更改?

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    使用 ImageBrush 有点棘手,我建议您创建新的 Image 元素并控制它。将 Image 和 ContentPresenter 放入 Grid。接下来,给Image命名,然后你就可以控制它了。之后,确保将 Image 设置为 NoResize,并将 Button Width&Height 绑定到 Image 的 ActualWidth 和 ActualHeight。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 2018-11-14
      • 1970-01-01
      • 2018-01-19
      相关资源
      最近更新 更多