wpf 平面按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2064185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
wpf flat button
提问by niao
How to make a button flat style in wpf? I've tried BasedOn property but it does not work.
如何在 wpf 中制作按钮平面样式?我已经尝试过 BaseOn 属性,但它不起作用。
回答by G. Ghez
More simple solution here using already defined ToolBar button style :
这里使用已定义的 ToolBar 按钮样式更简单的解决方案:
<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
Content="You know I'm flat..." />
回答by Eduardo Molteni
Just to get you started:
只是为了让您开始:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<Style x:Key="Flat">
<Setter Property="Control.Background" Value="{x:Null}" />
<Setter Property="Control.BorderBrush" Value="{x:Null}" />
<Style.Triggers>
<Trigger Property="Control.IsMouseOver" Value="True">
<Setter Property="Control.Background" Value="{x:Null}" />
<Setter Property="Control.BorderBrush" Value="{x:Null}" />
<Setter Property="Control.FontWeight" Value="Bold" />
</Trigger>
<Trigger Property="Control.IsFocused" Value="True">
<Setter Property="Control.FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</Page.Resources>
<StackPanel>
<Button Style="{StaticResource Flat}">Hello</Button>
</StackPanel>
</Page>
Then you have one millon other ways to do it, even changing the ControlTemplate
to complete redefine the Button
然后你有一百万种其他方法来做到这一点,甚至改变ControlTemplate
以完成重新定义按钮
回答by David Sherret
To add to Eduardo's answer, this solution gets rid of any extra styling such as the border around the button if the thickness is set to 0.
添加到 Eduardo 的答案中,如果厚度设置为 0,此解决方案将消除任何额外的样式,例如按钮周围的边框。
You can add extra styling as needed:
您可以根据需要添加额外的样式:
<Style x:Key="Flat" TargetType="Button">
<Setter Property="Background" Value="{x:Null}" />
<Setter Property="BorderBrush" Value="{x:Null}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="true">
</Trigger>
<Trigger Property="IsDefaulted" Value="true">
</Trigger>
<Trigger Property="IsPressed" Value="true">
</Trigger>
<Trigger Property="ToggleButton.IsChecked" Value="true">
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{x:Null}" />
<Setter Property="BorderBrush" Value="{x:Null}" />
<Setter Property="FontWeight" Value="Normal" />
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="FontWeight" Value="Normal" />
</Trigger>
</Style.Triggers>
</Style>
回答by Vijay Vepakomma
Quick solution: Embed the button in a <ToolBar/>
快速解决方案:将按钮嵌入 <ToolBar/>
Disclaimer: Will add a toolbar chrome, may be an issue in some cases. (Thanks Indeera)
免责声明:将添加工具栏镶边,在某些情况下可能是一个问题。(感谢英迪拉)
回答by Mario
If you just need to make the button "invisible" but highlightable:
如果您只需要使按钮“不可见”但可突出显示:
bb.Background = new SolidColorBrush(Colors.White); bb.BorderBrush = new SolidColorBrush(Colors.White);
bb.Background = new SolidColorBrush(Colors.White); bb.BorderBrush = new SolidColorBrush(Colors.White);