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. C#
  4. Create custom shaped buttons in WPF

Create custom shaped buttons in WPF

Scheduled Pinned Locked Moved C#
wpfcsharpcssdatabase
4 Posts 3 Posters 0 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.
  • A Offline
    A Offline
    abyclassic
    wrote on last edited by
    #1

    Hello, I need to create a custom shaped button which looks like a forward arrow. Three or four of such buttons are to be placed next to each other in such a way that the tip of the first button is just near the tail of the next button I have made a style for the button using polygon in .xaml & assigned this style to the button. Now, if i add this button to a form, i can see this button in the desired style. But, still the button has the rectangular shape, which is not visible as the borders & remaining area are transparent. So, the button occupies a rectangular area on the form irrespective of the style which i created. Because of this, i cannot keep the forward shaped butons next to each other, as close as desired. Here, for the tip of one forward button to come near the tail of the next button, the rectangular area of the buttons must overlap, which is not correct. Is there any way to remove the rectangular area of the button & make it exactly to occupy the desired shape as specified by the style. The .xaml file for my code is given below

        	<Setter Property="Template">
        		<Setter.Value>
        			<ControlTemplate TargetType="{x:Type Button}">
        				<Grid>
                            <Polygon Fill="{TemplateBinding Background}"  Points="10,10 80,10 120,45 80,75 10,75 45,45" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="2"/>
        					<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
        				</Grid>
        				<ControlTemplate.Triggers>
        					<Trigger Property="IsFocused" Value="True"/>
        					<Trigger Property="IsDefaulted" Value="True"/>
        					<Trigger Property="IsMouseOver" Value="True"/>
        					<Trigger Property="IsPressed" Value="True"/>
        					<Trigger Property="IsEnabled" Value="False"/>
        				</ControlTemplate.Triggers>
        			</ControlTemplate>
        		</Setter.Value>
        	</Setter>
        
        <Button Style="{DynamicResource P
    
    OriginalGriffO A 2 Replies Last reply
    0
    • A abyclassic

      Hello, I need to create a custom shaped button which looks like a forward arrow. Three or four of such buttons are to be placed next to each other in such a way that the tip of the first button is just near the tail of the next button I have made a style for the button using polygon in .xaml & assigned this style to the button. Now, if i add this button to a form, i can see this button in the desired style. But, still the button has the rectangular shape, which is not visible as the borders & remaining area are transparent. So, the button occupies a rectangular area on the form irrespective of the style which i created. Because of this, i cannot keep the forward shaped butons next to each other, as close as desired. Here, for the tip of one forward button to come near the tail of the next button, the rectangular area of the buttons must overlap, which is not correct. Is there any way to remove the rectangular area of the button & make it exactly to occupy the desired shape as specified by the style. The .xaml file for my code is given below

          	<Setter Property="Template">
          		<Setter.Value>
          			<ControlTemplate TargetType="{x:Type Button}">
          				<Grid>
                              <Polygon Fill="{TemplateBinding Background}"  Points="10,10 80,10 120,45 80,75 10,75 45,45" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="2"/>
          					<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
          				</Grid>
          				<ControlTemplate.Triggers>
          					<Trigger Property="IsFocused" Value="True"/>
          					<Trigger Property="IsDefaulted" Value="True"/>
          					<Trigger Property="IsMouseOver" Value="True"/>
          					<Trigger Property="IsPressed" Value="True"/>
          					<Trigger Property="IsEnabled" Value="False"/>
          				</ControlTemplate.Triggers>
          			</ControlTemplate>
          		</Setter.Value>
          	</Setter>
          
          <Button Style="{DynamicResource P
      
      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Don't post this is in both the forums and Q&A - pick one and stick with it.

      Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • A abyclassic

        Hello, I need to create a custom shaped button which looks like a forward arrow. Three or four of such buttons are to be placed next to each other in such a way that the tip of the first button is just near the tail of the next button I have made a style for the button using polygon in .xaml & assigned this style to the button. Now, if i add this button to a form, i can see this button in the desired style. But, still the button has the rectangular shape, which is not visible as the borders & remaining area are transparent. So, the button occupies a rectangular area on the form irrespective of the style which i created. Because of this, i cannot keep the forward shaped butons next to each other, as close as desired. Here, for the tip of one forward button to come near the tail of the next button, the rectangular area of the buttons must overlap, which is not correct. Is there any way to remove the rectangular area of the button & make it exactly to occupy the desired shape as specified by the style. The .xaml file for my code is given below

            	<Setter Property="Template">
            		<Setter.Value>
            			<ControlTemplate TargetType="{x:Type Button}">
            				<Grid>
                                <Polygon Fill="{TemplateBinding Background}"  Points="10,10 80,10 120,45 80,75 10,75 45,45" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="2"/>
            					<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
            				</Grid>
            				<ControlTemplate.Triggers>
            					<Trigger Property="IsFocused" Value="True"/>
            					<Trigger Property="IsDefaulted" Value="True"/>
            					<Trigger Property="IsMouseOver" Value="True"/>
            					<Trigger Property="IsPressed" Value="True"/>
            					<Trigger Property="IsEnabled" Value="False"/>
            				</ControlTemplate.Triggers>
            			</ControlTemplate>
            		</Setter.Value>
            	</Setter>
            
            <Button Style="{DynamicResource P
        
        A Offline
        A Offline
        Abhinav S
        wrote on last edited by
        #3

        Using a Path object, you can make your button look like exactly what you want to to be. See here[^] for more information.

        A 1 Reply Last reply
        0
        • A Abhinav S

          Using a Path object, you can make your button look like exactly what you want to to be. See here[^] for more information.

          A Offline
          A Offline
          abyclassic
          wrote on last edited by
          #4

          Hello Abhinav, Thanks for the reply. If you noticed my query, i am already making some custom shaped button. The actual requirement is about alignment of the controls, close to each other. Any control in .NET has a rectangular shape. We can see a rectangular shape around any control while adding to the UI. My question is regarding avoiding this rectangular shape. This alignment can be done by adding 2 of such buttons in a canvas or a single cell in a datagrid, by making the controls ovelapping each other (not the graphical area, only the transparent rectangular part of the control). But, if i need to add them in such a way in stack panel or any other container which adds controls next to each other, the second control is added only after the transparent rectangular part of the control. So, i am lloking for an option to change the underlying rectangular layout, whcih is not visible at run time. Is there any way to achieve this?

          Best Regards, Abhilash

          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