c# - 是否可以使用 XAML 样式覆盖嵌套控件的一组 DependencyProperty?
问题描述
让我们假设有Control
一个默认值Style
,我只能基于或覆盖。在 thisStyle
中,有 a ControlTemplate
which 有另一个Control
并直接设置Value
a 的 a 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
, 的ChildControl
不能被简单的覆盖。Style-Setter
<!-- Doesen't Work -->
<Style TargetType="{x:type ChildControl}">
<Setter Property="Property" Value="Value"/>
</Style>
但是根据相同的来源,可以用一个Animation
(如果动画永远持续下去)覆盖它。
就在那里,我被困住了。IsDirectionReversed
Property
更准确地说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
吗?或者-通过设置- 时Track
-的行为Control
不会改变?IsDirectionReversed
Property
Animation
解决方案
尝试定义一种隐式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>
推荐阅读
- c# - 如何使用 ASP.NET Core 对外部 API 进行集成测试
- android - 如何使用 Executors 作为 AsyncTask 的替代方法从 android 的数据库中获取数据?
- reactjs - 找不到模块'webpack-cli/bin/config-yargs' - React
- php - 如何在php中的复选框值处获取多个数据?
- python - 在 Windows 10 和 Ubuntu 18.04 lts 上使用 pip install 时出错
- pine-script - 在日线图上在一天结束时打开订单并在第二天打开时关闭订单的策略
- javascript - rss 不会解析
- javascript - 打印 consloge.log() 状态
- android - 在Android中使用列表与for循环插入房间
- sql - 在 postgresql 中自动生成字母数字 (AANNNN) id