5

我想访问嵌套在样式的控件模板中的属性。我知道您可以在代码隐藏中执行此操作:

GradientStop stop = (GradientStop)progressBar1.Template.FindName("gradStop", progressBar1);
stop.Color = Colors.Black;

是否可以做同样的事情,但在 XAML 中?例如:

<ProgressBar Style="{StaticResource CustomProgressBar}" [???].Color="FF000000"/>
4

1 回答 1

2

不能使用 TemplateBinding 吗?

    <Style x:Key="MyStyle" TargetType="{x:Type ContentControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ContentControl}">
                    <Border Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Background="{TemplateBinding Background}" >
                        <ContentPresenter />
                    </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后在应用样式时指定模板绑定值。

于 2013-03-28T15:05:27.123 回答