Accessing ProgressBar within ControlTemplate of Bu

2019-09-05 23:36发布

问题:

I am wondering how to access my progressbar within a button.

The control template used by the button is:

    <ControlTemplate x:Key="ButtonWithProgressBarControlTemplate" TargetType="{x:Type Button}">
    <Grid OpacityMask="Black">
        <Rectangle x:Name="rectangle" Fill= "{TemplateBinding Background}" RadiusX="8" RadiusY="8" Margin="0,0,0,0" OpacityMask="Black">
            <Rectangle.Effect>
                <DropShadowEffect ShadowDepth="2" />
            </Rectangle.Effect>
        </Rectangle>
        <ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RenderTransformOrigin="0.5,0.5">
        </ContentPresenter>
        <ProgressBar Name="buttonProgressBar" HorizontalAlignment="Left" Height="11" Margin="5,46,0,0" VerticalAlignment="Top" Width="150"/>
    </Grid>
</ControlTemplate>

I have tried this C# code which returns null:

ProgressBar progressBar = button.Template.FindName("buttonProgressBar", button) as ProgressBar;

This is based on other answers found on StackOverflow, so I am hoping it is something simple (but I do not see it).