首页 > 解决方案 > 是否可以使用 XAML 样式覆盖嵌套控件的一组 DependencyProperty?

问题描述

让我们假设有Control一个默认值Style,我只能基于或覆盖。在 thisStyle中,有 a ControlTemplatewhich 有另一个Control并直接设置Valuea 的 a DependencyProperty

像这样的东西:

<Style TargetType="{x:Type ParentControl}" x:Key="Test">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ParentControl}">
                <ChildControl Property="Value" ... />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

现在我想Value在不访问/更改默认值的情况下Property更改。ChildControlStyle

如果我没记错的话,由于Value PrecedenceForeground, 的ChildControl不能被简单的覆盖。Style-Setter

<!-- Doesen't Work -->
<Style TargetType="{x:type ChildControl}">
    <Setter Property="Property" Value="Value"/>
</Style>

但是根据相同的来源,可以用一个Animation(如果动画永远持续下去)覆盖它。

就在那里,我被困住了。IsDirectionReversed Property更准确地说ScrollBar:我无法覆盖 Track.ScrollViewer

<ScrollViewer Height="300" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ScrollViewer.Resources>
        <Style TargetType="{x:Type Track}">
            <Style.Triggers>
                <Trigger Property="IsVisible" Value="True">
                    <Trigger.EnterActions>
                        <BeginStoryboard x:Name="SetValue">
                            <Storyboard Storyboard.TargetProperty="(Track.IsDirectionReversed)">
                                <BooleanAnimationUsingKeyFrames RepeatBehavior="Forever" Duration="24:00:00">
                                    <DiscreteBooleanKeyFrame Value="False" KeyTime="00:00:00"/>
                                </BooleanAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <StopStoryboard BeginStoryboardName="SetValue"/>
                    </Trigger.ExitActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ScrollViewer.Resources>
    <Rectangle Height="800"/>
</ScrollViewer>

Trigger/有什么问题Animation吗?或者-通过设置- 时Track-的行为Control不会改变?IsDirectionReversedPropertyAnimation

标签: c#wpfxaml.net-4.6.1

解决方案


尝试定义一种隐式ScrollBar样式并将该Track样式放入该样式的Resources字典中。然后你的样式应该被应用到Track元素上:

<ScrollViewer Height="300" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ScrollViewer.Resources>
        <Style TargetType="ScrollBar">
            <Style.Resources>
                <Style TargetType="{x:Type Track}">
                    <Style.Triggers>
                        <Trigger Property="IsVisible" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard x:Name="SetValue">
                                    <Storyboard Storyboard.TargetProperty="(Track.IsDirectionReversed)">
                                        <BooleanAnimationUsingKeyFrames RepeatBehavior="Forever" Duration="24:00:00">
                                            <DiscreteBooleanKeyFrame Value="False" KeyTime="00:00:00"/>
                                        </BooleanAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <StopStoryboard BeginStoryboardName="SetValue"/>
                            </Trigger.ExitActions>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Style.Resources>
        </Style>
    </ScrollViewer.Resources>
    <Rectangle Height="800" Fill="Red"/>
</ScrollViewer>

推荐阅读