【问题标题】:Setting a property of a style's ControlTemplate in XAML在 XAML 中设置样式的 ControlTemplate 的属性
【发布时间】:2013-03-27 19:15:28
【问题描述】:

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

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

是否可以这样做,但在 XAML 中?例如:

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

【问题讨论】:

  • 据我所知你应该使用动态资源。
  • DynamicResource 可能是答案,是的,如果 ControlTemplate 构建正确。但是,如果您使用的是自定义模板,为什么还要尝试这样做?

标签: wpf xaml styles controltemplate


【解决方案1】:

可以不用 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>

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    相关资源
    最近更新 更多