Search code examples
c#wpfxamlbuttonprogress-bar

How to Put ProgressBar in Button


I am a newbie at WPF and did not pull off my attempt at putting a progressbar in a button. My previous question asked is at : StackOverflow Post. Any help with how to properly do this would be appreciated. I did not see another post on StackOverflow to address this. Ideally, it would be great to have the button (1) with text centered vertically (i.e. as appears normally in a button) and (2) with the progress bar below the text. The button would be 2 times default height to allow space for the progressbar. I can tweak position and spacing of anything you come up with. I am very interested in getting the bindings done correctly to update the progressbar.

Thanks! Buck


Solution

  • You have two options, either create a UserControl or a new style for your button and override the ControlTemplate. I would recommend that you style Button since its easier and you don't end up creating a new type. Here's how you would do that.

    <Button>
        <Button.Style>
            <Style BasedOn="{StaticResource {x:Type Button}}" TargetType="Button">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <!-- Put in your textbox and progress bar and what not -->
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>   
        </Button.Style>
    </Button>
    

    You can read more about templating controls here and can find the default Style and ControlTemplate for Button here.