Style.Trigger与DataTemplate.trigger(样式触发器与模板触发器)

以下样式触发器与模板触发器是等效的。

使用Style.Triggers:

        <Style x:Key="gateStyle" TargetType="Line">
            <Style.Triggers>
             <DataTrigger Binding="{Binding Direction}" Value="North">
                    <Setter Property="Margin" TargetName="line" Value="50 20 0 0"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Direction}" Value="South">
                    <Setter Property="Margin" TargetName="line" Value="50 80 0 0"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Direction}" Value="West">
                    <Setter TargetName="line" Property="Margin" Value="20 50 0 0"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Direction}" Value="East">
                    <Setter TargetName="line" Property="Margin" Value="80 50 0 0"/>
                </DataTrigger>
             </Style.Triggers>
         </Style>

...

            <Line Name="line" StrokeThickness="10" Stroke="Bisque" Margin="50 50 0 0"
                  X1="{Binding StartSectorPosition.X, Converter={StaticResource ArithmeticConverter}, ConverterParameter=*110}"
                  Y1="{Binding StartSectorPosition.Y, Converter={StaticResource ArithmeticConverter}, ConverterParameter=*110}"
                  X2="{Binding DestSectorPosition.X, Converter={StaticResource ArithmeticConverter}, ConverterParameter=*110}"
                  Y2="{Binding DestSectorPosition.Y, Converter={StaticResource ArithmeticConverter}, ConverterParameter=*110}"
                  Style="{StaticResource gateStyle}"/>

使用DataTemplate.trigger:

        <DataTemplate DataType="{x:Type WpfApplication1:Gate}">
            <Line Name="line" StrokeThickness="10" Stroke="Bisque" Margin="50 50 0 0"
                  X1="{Binding StartSectorPosition.X, Converter={StaticResource ArithmeticConverter}, ConverterParameter=*110}"
                  Y1="{Binding StartSectorPosition.Y, Converter={StaticResource ArithmeticConverter}, ConverterParameter=*110}"
                  X2="{Binding DestSectorPosition.X, Converter={StaticResource ArithmeticConverter}, ConverterParameter=*110}"
                  Y2="{Binding DestSectorPosition.Y, Converter={StaticResource ArithmeticConverter}, ConverterParameter=*110}"/>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Direction}" Value="North">
                    <Setter Property="Margin" TargetName="line" Value="50 20 0 0"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Direction}" Value="South">
                    <Setter Property="Margin" TargetName="line" Value="50 80 0 0"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Direction}" Value="West">
                    <Setter TargetName="line" Property="Margin" Value="20 50 0 0"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Direction}" Value="East">
                    <Setter TargetName="line" Property="Margin" Value="80 50 0 0"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>


你可能感兴趣的:(Style.Trigger与DataTemplate.trigger(样式触发器与模板触发器))