如何在xaml中定义矩形样式?

时间:2015-04-27 14:36:49

标签: xaml

我想避免在“ZoomInButton”和“ZoomOutButton”样式中重复标记“Rectangle”和“VisualStateManager.VisualStateGroups”。我该怎么做?

我尝试用targetType = "Button"定义一个样式,但它没有用。 还有另外一种方法吗?

<Style x:Key="ZoomInButton" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>                
            <ControlTemplate TargetType="Button">
                <Grid x:Name="pr7">
                    <StackPanel>
                        <Image Source="/Images/zoomIn.png" HorizontalAlignment="Center" VerticalAlignment="Center" Opacity="0.8"/>
                    </StackPanel>
                    <Rectangle
                        x:Name="fvw"
                        Margin="0"
                        Stretch="Fill"
                        IsHitTestVisible="False"
                        Stroke="White"                            
                        StrokeDashArray="1,5"
                        Opacity="0"
                    />
                    <Rectangle
                        x:Name="fvb"
                        Margin="0"
                        Stretch="Fill"
                        IsHitTestVisible="False"
                        Stroke="Black"
                        StrokeDashArray="1,1"
                        Opacity="0"
                    />
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="prova">
                            <VisualState x:Name="Focused">
                                <Storyboard>
                                    <DoubleAnimation
                                        Storyboard.TargetName="fvw"
                                        Storyboard.TargetProperty="Opacity"
                                        To="1"
                                        Duration="0"/>
                                    <DoubleAnimation
                                        Storyboard.TargetName="fvb"
                                        Storyboard.TargetProperty="Opacity"
                                        To="1"
                                        Duration="0"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Unfocused" />
                            <VisualState x:Name="PointerFocused" />
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="ZoomOutButton" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid x:Name="pr6">
                    <StackPanel>
                        <Image Source="/Images/zoomOut.png" Opacity="0.8"/>
                    </StackPanel>
                    <Rectangle
                        x:Name="fvw"
                        Margin="0"
                        Stretch="Fill"
                        IsHitTestVisible="False"
                        Stroke="White"                            
                        StrokeDashArray="1,5"
                        Opacity="0"
                    />
                    <Rectangle
                        x:Name="fvb"
                        Margin="0"
                        Stretch="Fill"
                        IsHitTestVisible="False"
                        Stroke="Black"
                        StrokeDashArray="1,1"
                        Opacity="0"
                    />
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="prova">
                            <VisualState x:Name="Focused">
                                <Storyboard>
                                    <DoubleAnimation
                                        Storyboard.TargetName="fvw"
                                        Storyboard.TargetProperty="Opacity"
                                        To="1"
                                        Duration="0"/>
                                    <DoubleAnimation
                                        Storyboard.TargetName="fvb"
                                        Storyboard.TargetProperty="Opacity"
                                        To="1"
                                        Duration="0"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Unfocused" />
                            <VisualState x:Name="PointerFocused" />
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                </Grid>
            </ControlTemplate>
        </Setter.Value>  
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:0)

两个按钮的常见样式通常会使用TemplateBinding将Image的awk '{sum+=/Time in seconds/$5; print sum/NR}' bt.B.1.log 属性绑定到Button的Source属性:

Content

您可以这样使用它:

<Style x:Key="ZoomButton" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid x:Name="pr7">
                    <StackPanel>
                        <Image Source="{TemplateBinding Content}" .../>
                    </StackPanel>
                    <Rectangle x:Name="fvw" ... />
                    <Rectangle x:Name="fvb" ... />
                    <VisualStateManager.VisualStateGroups>
                        ...
                    </VisualStateManager.VisualStateGroups>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

如果您需要在代码中设置Content属性,请在Button上设置<Button Style="{StaticResource ZoomButton}" Content="/Images/zoomIn.png" .../> <Button Style="{StaticResource ZoomButton}" Content="/Images/zoomOut.png" .../> 属性

x:Name

并在以下代码中使用它:

<Button x:Name="zoomInButton" .../>