【问题标题】:Removing border style from disabled button从禁用按钮中删除边框样式
【发布时间】:2013-10-31 16:29:44
【问题描述】:

我有一个包含Button 的用户控件,然后这个按钮包含一个Image 和一个TextBlock。这使我有一个图像按钮。我还希望按钮具有无边框样式,这是通过将按钮 BorderBrush 设置为 null 来实现的。这部分一切正常。

我遇到的问题是当我想“禁用”用户控件时。我可以通过将IsEnabled 属性设置为false 来做到这一点,它会向下级联到按钮并适当地防止按钮被单击。但是,当按钮被禁用时,它会显示一个我不想要的边框。

问题:当按钮被禁用时如何去掉它的边框?

我尝试了几种不同的方法,例如从代码中设置边框:

MyButton.BorderBrush = Brushes.Transparent;

并在 XAML 中使用样式:

<Button.Style>
    <Style TargetType="Button">
        <Setter Property="BorderBrush" Value="{x:Null}"/>
    </Style>
</Button.Style>

还有其他一些事情只是为了尝试让某些东西发挥作用,但到目前为止没有任何改变。

注意:我已经尝试了上述两种使用 Opacity 属性的解决方案,并且在这方面都可以正常工作,但是当我尝试更改边框时却不行

【问题讨论】:

  • 窃取微软按钮的风格,改变它,认领它。

标签: c# wpf button border


【解决方案1】:

不幸的是,在 Button 禁用时应用到 StyleStyle 在默认 ControlTemplate 中应用。这意味着如果你想改变它,你需要为你的Button 定义一个新的ControlTemplate。您可以在 MSDN 的 Button Styles and Templates 页面中找到默认的 ControlTemplate

在提供新的ControlTemplate 时,有时会遗漏原始模板中的部分内容,因此一个很好的起点是实现该页面上的默认ControlTemplate,然后根据自己的喜好对其进行“调整”。

【讨论】:

    【解决方案2】:

    由于您已经为 Button 提供了模板,因此您需要插入带有 Property="IsEnabled" Value="False"Trigger

    ControlTemplate 的示例Button

    <ControlTemplate TargetType="{x:Type Button}">
    <Border x:Name="border" TextBlock.Foreground="{StaticResource Button.Static.Foreground}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                            <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
    </Border>
    <ControlTemplate.Triggers>
    <Trigger Property="IsDefaulted" Value="true">
                                <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
    
    </Trigger>
    <Trigger Property="IsMouseOver" Value="true">
    <Setter Property="Background"           TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/>
    <Setter Property="BorderBrush"          TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>
    <Setter Property="TextBlock.Foreground" TargetName="border" Value="{StaticResource Button.MouseOver.Foreground}" />
    </Trigger>
    <Trigger Property="IsPressed" Value="true">
    <Setter Property="Background"           TargetName="border" Value="{StaticResource Button.Pressed.Background}"/>
    <Setter Property="BorderBrush"          TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>
    <Setter Property="TextBlock.Foreground" TargetName="border" Value="{StaticResource Button.Pressed.Foreground}" />
    </Trigger>
    
    //Here's the code where you want to change the border
    //Change the Value part to whatever you want.
    
    <Trigger Property="IsEnabled" Value="false">
    <Setter Property="Background"             TargetName="border"           Value="{StaticResource Button.Disabled.Background}"/>
    <Setter Property="BorderBrush"            TargetName="border"           Value="{StaticResource Button.Disabled.Border}"/>
    <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/>
    </Trigger>
    </ControlTemplate.Triggers>
    </ControlTemplate>
    

    对不起,缩进,但我相信你会实现你想要的!

    【讨论】:

      猜你喜欢
      • 2015-09-01
      • 2012-07-14
      • 1970-01-01
      • 2020-03-27
      • 2021-07-25
      • 2015-12-16
      • 2013-12-18
      相关资源
      最近更新 更多