[英]Is it possible to override a set DependencyProperty of a nested Control with a XAML-Style?
假設有一個帶有默認Style
的Control
,我只能基於它或重寫它。 在這種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>
現在我想改變Value
的的Property
在ChildControl
得不到/更改默認Style
。
如果我沒記錯的話,由於Value Precedence , ChildControl
的Foreground
無法用簡單的Style-Setter
覆蓋。
<!-- 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
嗎? 還是通過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.