簡體   English   中英

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

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

假設有一個帶有默認StyleControl ,我只能基於它或重寫它。 在這種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>

現在我想改變Value的的PropertyChildControl得不到/更改默認Style

如果我沒記錯的話,由於Value PrecedenceChildControlForeground無法用簡單的Style-Setter覆蓋。

<!-- 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 - Property時, Track 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM