Change shape of WPF button without changing other styles

后端 未结 3 1742
耶瑟儿~
耶瑟儿~ 2021-01-04 23:56

I would like to change the shape of a WPF button from the default round rectangle to another shape (say, an arrow), but I want to keep the rest of the styling -- fill color,

相关标签:
3条回答
  • 2021-01-05 00:29

    If you want to inherit the default system colours/styles for a Button you can use TemplateBindings within the button Template. For example, if you wanted to create a custom-shaped button that had the same background colour as the system one, you could use the below:

    <Button>
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <Grid>
                    <!-- circle to have inherited colour -->
                    <Ellipse Fill="{TemplateBinding Background}" Height="50" Width="50" />
                    <ContentPresenter />
                </Grid>
            </ControlTemplate>
        </Button.Template>
    </Button>
    

    The Ellipse will then inherit the background colour from the Button, however it is set.

    0 讨论(0)
  • 2021-01-05 00:38

    You should be able to subclass the normal button control. You can then customize the subclassed button with a custom shape, but all other properties would be inherited. This would allow you to change the shape for your button and nothing else.

    Here's a good resource for creating a custom button if you have Blend:
    http://msdn.microsoft.com/en-us/library/bb613598.aspx

    0 讨论(0)
  • 2021-01-05 00:42

    Make a copy of the default Button Template, and simply change the default panel to your own custom shape.

    In the case of the button, it looks like you need to replace the Border with something like a Path defining an Arrow.

    Since a Path doesn't have content, you'll probably have to put both the shape and the content in another panel which allows objects to overlap, such as a Grid.

    So your end result will look something like this

    <Grid>
        <Path />
        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Grid>
    

    instead of the default

    <Button>
        <ContentPresenter />
    </Button>
    
    0 讨论(0)
提交回复
热议问题