是否可以用 XAML 样式覆盖嵌套控件的一组 DependencyProperty?

Is it possible to override a set DependencyProperty of a nested Control with a XAML-Style?

让我们假设有一个 Control 和默认值 Style,我只能基于或覆盖它。在这个 Style 中有一个 ControlTemplate,它有另一个 Control 并直接设置 DependencyPropertyValue

像这样:

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

现在我想将 ChildControl 中的 PropertyValue 更改为没有 access/changing 的默认值 Style

如果我没记错的话,ChildControlForeground 不能被简单的 Style-Setter 覆盖,因为 Value Precedence.

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

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

就在那里,我卡住了。更准确地说:我无法在 ScrollViewer 中覆盖垂直 ScrollBar-TrackIsDirectionReversed Property

<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有问题吗?或者当通过 Animation 设置 IsDirectionReversed-PropertyTrack-Control 的行为不会改变?

尝试定义一个隐含的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>