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

时间:2019-02-13 13:57:14

标签: c# wpf xaml .net-4.6.1

让我们假设有一个Control和一个默认Style,我只能基于它或重写它。在此Style中,有一个ControlTemplate,其中还有另一个Control,并直接设置Value中的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中的ChildControl,而无需访问/更改默认的Style

如果我没记错的话,由于Value PrecedenceForeground的{​​{1}}不能被简单的ChildControl覆盖。

Style-Setter

但是根据同一来源,可以使用<!-- Doesen't Work --> <Style TargetType="{x:type ChildControl}"> <Setter Property="Property" Value="Value"/> </Style> 覆盖它(如果动画永远持续下去)。

就在那儿,我被卡住了。更精确地说:我无法覆盖Animation中垂直IsDirectionReversed-Property的{​​{1}} 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-TrackControl-IsDirectionReversed的行为不会改变吗?

1 个答案:

答案 0 :(得分: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>