【问题标题】:Is it possible to override a set DependencyProperty of a nested Control with a XAML-Style?是否可以使用 XAML 样式覆盖嵌套控件的一组 DependencyProperty?
【发布时间】:2019-02-13 13:57:13
【问题描述】:

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

类似这样的:

<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 中Property 的Value,而不访问/更改默认Style。

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

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

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

就在那里,我被困住了。更准确地说:我不能在ScrollViewer 中覆盖垂直ScrollBar-Track 的IsDirectionReversed 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 有问题吗?或者当IsDirectionReversed-Property 通过Animation 设置时Track-Control 的行为不会改变?

【问题讨论】:

    标签: c# wpf xaml .net-4.6.1


    【解决方案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>
    

    【讨论】:

    • 非常感谢!是否因为默认 ScrollViewer 是基于 ControlTemplate 而不是 Style 而未应用样式?或者为什么解决了隐含的Style问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 2019-11-08
    • 2019-10-02
    相关资源
    最近更新 更多