Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Flat Button Style Problem

Flat Button Style Problem

Scheduled Pinned Locked Moved WPF
databasehelp
6 Posts 2 Posters 21 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I'm trying to create a flat button style: Style

    <Style x:Key="buttonStyle" 
           TargetType="{x:Type Button}">
        
        <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Margin" Value="0"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Height" Value="25"/>
        <Setter Property="Width" Value="75"/>
    
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
    
                    <Border x:Name="border"
                            Margin="2" 
                            Background="{TemplateBinding Background}" 
                            BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="{TemplateBinding BorderThickness}" 
                            CornerRadius="{TemplateBinding BorderThickness}">
    
                        <ContentPresenter x:Name="contentPresenter"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                          RecognizesAccessKey="True" 
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          TextElement.Foreground="{TemplateBinding Foreground}"/>
                        
                    </Border>
                    
                    <ControlTemplate.Triggers>
                        
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="border" Property="BorderBrush" Value="{StaticResource Button.Hover.Foreground}" />
                            <Setter TargetName="border" Property="BorderThickness" Value="1" />
                        </Trigger>
    
                        <Trigger Property="IsPressed" Value="true">
                            <Setter TargetName="border" Property="BorderBrush" Value="{StaticResource Button.Pressed.Foreground}" />
    
    L 1 Reply Last reply
    0
    • K Kevin Marois

      I'm trying to create a flat button style: Style

      <Style x:Key="buttonStyle" 
             TargetType="{x:Type Button}">
          
          <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
          <Setter Property="HorizontalContentAlignment" Value="Center"/>
          <Setter Property="VerticalContentAlignment" Value="Center"/>
          <Setter Property="Margin" Value="0"/>
          <Setter Property="Padding" Value="0"/>
          <Setter Property="BorderThickness" Value="0"/>
          <Setter Property="Height" Value="25"/>
          <Setter Property="Width" Value="75"/>
      
          <Setter Property="Template">
              <Setter.Value>
                  <ControlTemplate TargetType="{x:Type Button}">
      
                      <Border x:Name="border"
                              Margin="2" 
                              Background="{TemplateBinding Background}" 
                              BorderBrush="{TemplateBinding BorderBrush}" 
                              BorderThickness="{TemplateBinding BorderThickness}" 
                              CornerRadius="{TemplateBinding BorderThickness}">
      
                          <ContentPresenter x:Name="contentPresenter"
                                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                            RecognizesAccessKey="True" 
                                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                            TextElement.Foreground="{TemplateBinding Foreground}"/>
                          
                      </Border>
                      
                      <ControlTemplate.Triggers>
                          
                          <Trigger Property="IsMouseOver" Value="true">
                              <Setter TargetName="border" Property="BorderBrush" Value="{StaticResource Button.Hover.Foreground}" />
                              <Setter TargetName="border" Property="BorderThickness" Value="1" />
                          </Trigger>
      
                          <Trigger Property="IsPressed" Value="true">
                              <Setter TargetName="border" Property="BorderBrush" Value="{StaticResource Button.Pressed.Foreground}" />
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Doesn't change from what to what? Is it "Red"?

      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

      K 1 Reply Last reply
      0
      • L Lost User

        Doesn't change from what to what? Is it "Red"?

        "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        Sorry I wasn't clear. I set it to red but it shows as black.

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        L 1 Reply Last reply
        0
        • K Kevin Marois

          Sorry I wasn't clear. I set it to red but it shows as black.

          If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You want "TextBlock" instead of TextElement, apparently: [https://stackoverflow.com/questions/249671/changing-the-foreground-colour-of-a-contentpresenter-in-a-listbox\](https://stackoverflow.com/questions/249671/changing-the-foreground-colour-of-a-contentpresenter-in-a-listbox)

          "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

          K 1 Reply Last reply
          0
          • L Lost User

            You want "TextBlock" instead of TextElement, apparently: [https://stackoverflow.com/questions/249671/changing-the-foreground-colour-of-a-contentpresenter-in-a-listbox\](https://stackoverflow.com/questions/249671/changing-the-foreground-colour-of-a-contentpresenter-in-a-listbox)

            "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

            K Offline
            K Offline
            Kevin Marois
            wrote on last edited by
            #5

            Nope, didn't work. I guess I could replace the ContentPresenter with a TextBlock, but then I'd need a DP behind it for the text. There's gotta be a XAML way to do this

            If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

            L 1 Reply Last reply
            0
            • K Kevin Marois

              Nope, didn't work. I guess I could replace the ContentPresenter with a TextBlock, but then I'd need a DP behind it for the text. There's gotta be a XAML way to do this

              If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              If content is a "string" (as in your case), the Button uses a TextBlock; if content is some UI element composite, a "ForeGround" color may make little sense. So, yes, I would have used a TextBlock (as "content") in the first place. (I sometimes use one or more FontIcons overlayed in a grid). (The "dynamic" resource binding also makes little sense in this case)

              "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups