2010-01-14 21 views
17

Làm cách nào để tạo kiểu nút phẳng trong wpf? Tôi đã thử thuộc tính BasedOn nhưng nó không hoạt động.wpf nút phẳng

+8

Xem thêm: http://stackoverflow.com/questions/697381/setting-button-flatstyle-in-wpf đặc biệt là nếu bạn thích một lớp lót. – greenoldman

Trả lời

22

Chỉ cần để giúp bạn bắt đầu:

<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> 

Sau đó, bạn có một cách Millon khác để làm điều đó, thậm chí thay đổi ControlTemplate để hoàn thành xác định lại nút

+2

Bạn không thể làm việc này. Kiểu mẫu của bạn không xóa đường viền hoặc nền của Nút. Điều duy nhất hoạt động từ ví dụ này là văn bản nút trở nên đậm khi di chuột. – Johncl

+0

@Johncl xem câu trả lời của tôi bên dưới –

1

giải pháp nhanh: Nhúng vào nút trong một <ToolBar/>

Tuyên bố từ chối trách nhiệm: Sẽ thêm chrome trên thanh công cụ, có thể là sự cố trong một số trường hợp. (Cảm ơn Indeera)

+0

Điều này thêm chrome thanh công cụ và không giải quyết được sự cố. – Indy9000

+0

Thêm thanh công cụ chrome: yes; Không giải quyết được vấn đề: chủ quan –

6

Để thêm vào câu trả lời của Eduardo, giải pháp này giúp loại bỏ của bất kỳ phong cách bổ sung như đường viền xung quanh nút nếu độ dày được thiết lập để 0.

Bạn có thể bổ sung thêm phong cách khi cần thiết:

<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> 
+0

Một cải tiến khác mà tôi sẽ thêm là đặt 'SnapsToDevicePixels =" True "' cho ''. Điều này sẽ loại bỏ bất kỳ đường viền mờ nào trong trường hợp người dùng đặt 'BorderThickness' thành giá trị khác 0. – DaveS

22

More đơn giản giải pháp ở đây bằng cách sử dụng đã được xác định nút ToolBar phong cách:

<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" 
     Content="You know I'm flat..." /> 
0

Nếu bạn chỉ cần làm cho nút "vô hình" nhưng nổi bật có thể:

bb.Background = new SolidColorBrush (Colors.White); bb.BorderBrush = new SolidColorBrush (Colors.White);