在Button的ControlTemplate中访问ProgressBar

时间:2013-05-05 19:34:25

标签: c# wpf xaml controls controltemplate

我想知道如何在按钮内访问我的进度条。

按钮使用的控件模板是:

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

我试过这个返回null的C#代码:

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

这是基于StackOverflow上的其他答案,所以我希望它很简单(但我没看到)。

0 个答案:

没有答案