风格触发器不会继承?

时间:2018-04-20 18:29:17

标签: wpf xaml triggers

我有一个定义了以下触发器的样式:

<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>

当我应用直接样式时,这非常有效。但是,我希望能够基于此样式创建样式以覆盖特定属性,如边距和颜色。我尝试按如下方式执行此操作(这会根据此样式创建默认值):

<Style TargetType="{x:Type graphicElements:MyTabItem}" BasedOn="{StaticResource StMyTabItemBase}" />

但是,只要我这样做,它就不再能够正确触发或切换模板。如果我只是将触发器复制到新样式中它再次起作用,但我不想单独将触发器放入每种样式中......为什么这些继承不像它们应该的那样?或者我是否误解了有关BasedOn和Triggers的内容?

修改

这是&#39; BasedOn&#39;有效的风格:

<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>

基本上我只是复制了基本样式的触发器部分。但是,我不明白为什么要这样做......

1 个答案:

答案 0 :(得分:1)

我不确定&#34;应该&#34;但你的退出故事板肯定不会找到第一个 我最近遇到过这种行为。使用名望镜是一件奇怪的事。这不会奏效。
您可以尝试启动另一个故事板,以0持续时间为目标同一属性,并希望停止它。这在我的情况下不起作用。

我在我的应用程序中解决这个问题的方法是使用多路转换器来启动和停止故事板 我的转换器:

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;
    }
}

您可能更喜欢启用附加属性。 用途:

<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>

我的故事板是一个单独的资源。

                     Storyboard.TargetProperty =&#34;(Shape.StrokeDashOffset)&#34;             RepeatBehavior =&#34;永远&#34;             从=&#34; 0&#34;             为了=&#34; 8&#34;             持续时间=&#34; 00:00:0.8&#34;             Timeline.DesiredFrameRate =&#34; 10&#34;                                          /&GT;

这在我的地图编辑器中使用(对于我们的游戏)。用户正在绘制场景的地图。他们绘制地形。树木,河流,轮廓等都是具有不同模板的多边形,我使用基于继承,所以所有人都会得到相同的&#34;行进蚂蚁&#34;选择地形进行编辑时,用虚线动画的动画将围绕它们进行动画处理。