使用StyleTriggers / DataTriggers的WPF样式继承

时间:2015-10-19 16:52:50

标签: c# wpf

我有一些类似于以下的形状样式:

 <Style TargetType="{x:Type Path}" x:Key="Triangle">        
    <Setter Property="Fill" Value="Orange" />          
    <Setter Property="Data" Value="M0,14L14,14 7,0 0,14z" />
 </Style>

根据某些数据值,我可能需要旋转的形状。为此,我将以下属性和触发器添加到样式:(这些正常工作)

 <Setter Property="RenderTransform">
        <Setter.Value>
            <ScaleTransform CenterX="5" CenterY="1" />
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <DataTrigger  Value="true" >
            <DataTrigger.Binding>
                <MultiBinding UpdateSourceTrigger="PropertyChanged">
                    <MultiBinding.Converter>
                        <conv:IsSpinningConverter />
                    </MultiBinding.Converter>
                    <Binding Path="Value" IsAsync="False" UpdateSourceTrigger="PropertyChanged" />
                    <Binding Path="IsAcknowledged" IsAsync="False" UpdateSourceTrigger="PropertyChanged" />
                </MultiBinding>
            </DataTrigger.Binding>
            <DataTrigger.EnterActions>
                <BeginStoryboard x:Name="Spin">
                    <Storyboard RepeatBehavior="Forever">
                        <DoubleAnimation Storyboard.TargetProperty="(Path.RenderTransform).(ScaleTransform.ScaleX)"
                            From="1" To=".0001" Duration="0:0:0.5"
                            RepeatBehavior="Forever" AutoReverse="True" />
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
            <DataTrigger.ExitActions>
                <RemoveStoryboard BeginStoryboardName="Spin" />
            </DataTrigger.ExitActions>
        </DataTrigger>
    </Style.Triggers>

将这一个属性和触发器添加到每个形状会感觉麻烦且效率低下。所以我原本以为我可以将RenderTransform属性和DataTriggers转换为它自己的属性,所有形状都可以这样建立:

 <Style TargetType="{x:Type Path}" x:Key="ShapeBase" >
    <Setter Property="RenderTransform">
        <Setter.Value>
            <ScaleTransform CenterX="5" CenterY="1" />
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <DataTrigger  Value="true" >
            <DataTrigger.Binding>
                <MultiBinding UpdateSourceTrigger="PropertyChanged">
                    <MultiBinding.Converter>
                        <conv:IsSpinningConverter />
                    </MultiBinding.Converter>
                    <Binding Path="Value" IsAsync="False" UpdateSourceTrigger="PropertyChanged" />
                    <Binding Path="IsAcknowledged" IsAsync="False" UpdateSourceTrigger="PropertyChanged" />
                </MultiBinding>
            </DataTrigger.Binding>
            <DataTrigger.EnterActions>
                <BeginStoryboard x:Name="Spin">
                    <Storyboard RepeatBehavior="Forever">
                        <DoubleAnimation Storyboard.TargetProperty="(Path.RenderTransform).(ScaleTransform.ScaleX)"
                            From="1" To=".0001" Duration="0:0:0.5"
                            RepeatBehavior="Forever" AutoReverse="True" />
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
            <DataTrigger.ExitActions>
                <RemoveStoryboard BeginStoryboardName="Spin" />
            </DataTrigger.ExitActions>
        </DataTrigger>
    </Style.Triggers>
</Style>

<Style TargetType="{x:Type Path}" x:Key="Triangle" BasedOn="{StaticResource ShapeBase}">        
    <Setter Property="Fill" Value="Orange" />  
    <Setter Property="Data" Value="M0,14L14,14 7,0 0,14z" />
 </Style>

当我这样做时,似乎DataTriggers不再发射,因为形状不再旋转。使用DataTriggers和样式继承是否存在问题?这里有什么东西可以防止DataTriggers被触发吗?

谢谢!

0 个答案:

没有答案