Accessing ProgressBar within ControlTemplate of Button

若如初见. 提交于 2019-12-12 00:59:45

问题


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).

来源:https://stackoverflow.com/questions/16388333/accessing-progressbar-within-controltemplate-of-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!