问题描述
我有一个定义了以下触发器的样式:
I have a style with the following triggers defined:
<Style TargetType="{x:Type graphicElements:MyTabItem}" x:Key="StMyTabItemBase">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="SelectedColor" Value="{Binding ApplicationColor, RelativeSource={RelativeSource AncestorType=graphicElements:MyTabControl}}" />
<Setter Property="Expanded" Value="False" />
<Setter Property="BorderBrush" Value="#FFACACAC"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="SnapsToDevicePixels" Value="False" />
<Setter Property="Template" Value="{StaticResource CtTabCollapsed}" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsSideBarExpanded, RelativeSource={RelativeSource AncestorType=graphicElements:MyTabControl}}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="Expanded" BeginTime="0" Duration="0">
<DiscreteBooleanKeyFrame Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="Expanded" BeginTime="0:0:0.25" Duration="0">
<DiscreteBooleanKeyFrame Value="False" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
<Trigger Property="Expanded" Value="True">
<Setter Property="Template" Value="{StaticResource CtTabExpanded}" />
</Trigger>
</Style.Triggers>
</Style>
当我应用直接样式时,这非常理想。但是,我希望能够基于此样式创建样式以覆盖诸如边距和颜色之类的特定属性。我尝试这样做(这将基于此样式创建默认值):
This works perfectly when I apply the direct style. However, I want to be able to create styles based on this style to override specific properties like margins and colors. I have tried to do so as follows (this creates a default based on this style):
<Style TargetType="{x:Type graphicElements:MyTabItem}" BasedOn="{StaticResource StMyTabItemBase}" />
但是,一旦我这样做,它将不再正确触发或切换模板。如果我仅将触发器复制到新样式中,则它可以再次使用,但是我不想将触发器分别放入每种样式中……为什么这些继承不如它们应该的那样进行?还是我误解了有关BasedOn和Triggers?
However, as soon as I do that it no longer triggers or switches the template properly. If I copy just the triggers into the new style it works again, but I don't want to have to put the triggers into each style individually... Why aren't these inheriting like they are supposed to? Or do I misunderstand something about BasedOn and Triggers?
EDIT
这里是有效的'BasedOn'样式:
Here is the 'BasedOn' style that works:
<Style TargetType="{x:Type graphicElements:AutodeskTabItem}" BasedOn="{StaticResource StAutodeskTabItemBase}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSideBarExpanded, RelativeSource={RelativeSource AncestorType=graphicElements:AutodeskTabControl}}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="Expanded" BeginTime="0" Duration="0">
<DiscreteBooleanKeyFrame Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="Expanded" BeginTime="0:0:0.25" Duration="0">
<DiscreteBooleanKeyFrame Value="False" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
<Trigger Property="Expanded" Value="True">
<Setter Property="Template" Value="{StaticResource CtTabExpanded}" />
</Trigger>
</Style.Triggers>
</Style>
基本上,我只是复制了基本样式的触发器部分。但是我不明白为什么我必须这样做...
Basically I just copied out the triggers portion of the base style. However I don't understand why I have to do that...
推荐答案
我不确定应该但是您的退出故事板肯定找不到第一个。
我最近遇到了这种行为。使用名称范围很奇怪。这是行不通的。
您可以尝试启动另一个针对同一属性的情节提要,且持续时间为0,并希望能够将其停止。就我而言,这不起作用。
I'm not sure about "supposed to" but your exit storyboard definitely won't find the first.
I recently came across this behaviour. It's an oddity to do with namescope. That won't work.
You could try starting another storyboard targeting the same property with 0 duration and hope that stops it. This did not work in my case.
在我的应用中解决此问题的方法是使用多转换器启动和停止情节提要。
我的转换器:
The way I work round this in my app is to use a multiconverter to start and stop the storyboard.
My converter:
public class MultiAnimatorConverter : MarkupExtension, IMultiValueConverter
{
public Storyboard sb { get; set; }
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
// 0 = Shape
// 1 = Tag
Shape shape = values[0] as Shape;
if(shape.Tag.ToString() == "False")
{
sb.Stop(shape);
sb.Remove(shape);
}
else
{
sb.Begin(shape, true);
}
return true; // Abusing isenabled so I don't need another attached dependency property
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return null;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
您可能希望启用附加属性。
使用情况:
You may prefer an attached property to enabled.Useage:
<Setter Property="IsEnabled">
<Setter.Value>
<MultiBinding Converter="{ui:MultiAnimatorConverter sb={StaticResource MarchinAntsAnimation}}">
<Binding RelativeSource="{RelativeSource Self}"/>
<Binding Path="Tag" RelativeSource="{RelativeSource Self}"/>
</MultiBinding>
</Setter.Value>
</Setter>
我的故事板是单独的资源。
My storyboard is a separate resource.
Storyboard.TargetProperty =(Shape.StrokeDashOffset)
RepeatBehavior = Forever
From = 0
To = 8
Duration = 00:00:.8
Timeline.DesiredFrameRate = 10
/>
Storyboard.TargetProperty="(Shape.StrokeDashOffset)" RepeatBehavior="Forever" From="0" To="8" Duration="00:00:.8" Timeline.DesiredFrameRate="10" />
这在我的地图编辑器中使用(针对我们的游戏)。用户正在绘制方案的地图。他们绘制地形。树林,河流,等高线等都是具有不同模板的多边形,我使用基于来继承,因此在选择地形进行编辑时,所有人都将得到相同的带有虚线动画的行进蚂蚁动画。
This is used in my Map Editor ( for our game ). The user is drawing the map for a scenario. They draw terrain. Woods, rivers, contours etc. All are polygons with different templates and I use basedon to inherit so all would get the same "marching ants" animation with dotted lines animated round them when the terrain is selected for editing.
这篇关于样式触发器不继承吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!