【问题标题】:How to disable border of WPF button when click it?单击它时如何禁用WPF按钮的边框?
【发布时间】:2009-11-26 04:31:39
【问题描述】:

当我点击它时如何禁用 WPF 按钮的边框?

我有如下创建按钮,一切正常,除非我点击按钮。

<Button Background="Transparent" BorderBrush="Transparent">
    <Button.Content>
        <StackPanel>
            <Image Source="xxx.png" />
            <TextBlock Text="Change Password" />
        </StackPanel>
    </Button.Content>
</Button>

当我点击按钮时,它的边框如下所示。

alt text http://www.freeimagehosting.net/uploads/8ece306bd4.png

我尝试为按钮的FocusVisualStyle 创建样式,但它没有按预期工作,当我设置IsDefault="True" 时也会出现此问题。

【问题讨论】:

    标签: wpf styles


    【解决方案1】:

    我知道这是一个老问题,但我觉得我可以回答。

    如果我对问题的理解正确,在您单击按钮并继续前进后,其周围会保留一个边框。当您单击其他项目时,例如 TextBox 或其他 Button,边框会消失。

    这个“边框”是“焦点”指示器。

    为防止这种情况发生,请将按钮上的“Focusable”设置为“False”:

    <Button Background="Transparent" BorderBrush="Transparent" Focusable="False">
        <Button.Content>
            <StackPanel>
                <Image Source="xxx.png" />
                <TextBlock Text="Change Password" />
            </StackPanel>
        </Button.Content>
    </Button>
    

    【讨论】:

    • 我在找这个...thnx
    • 这个答案太疯狂了。每个人都知道 WPF 中的简单解决方案需要 300 行代码和一个依赖属性
    • 这对于使用键盘导航的人来说不是一个好的解决方案,因为这不允许您使用制表符来聚焦按钮。
    【解决方案2】:

    您可能需要更改按钮模板,这将为您提供一个没有框架的按钮,但也没有任何按下或禁用效果:

    在您的 Window.Resources 元素中:

    <Style TargetType="Button" x:Key="TransparentButton">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Background="Transparent">
                       <ContentPresenter/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    还有按钮:

    <Button Style="{StaticResource TransparentButton}">
        <Button.Content>
            <StackPanel>
                <Image Source="xxx.png" />
                <TextBlock Text="Change Password" />
            </StackPanel>
        </Button.Content>
    </Button>
    

    现在,如果您需要更多视觉反馈,请从这个模板开始:

    http://msdn.microsoft.com/en-us/library/ms753328.aspx

    并删除东西,直到你得到你想要的。

    不要忘记为您的元素添加透明背景,如果您没有透明背景,或者背景为空,则按钮内的透明区域将无法点击。

    【讨论】:

      猜你喜欢
      • 2019-08-15
      • 2022-08-16
      • 2020-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      相关资源
      最近更新 更多