如何在WPF中使用XAML进行加号?

时间:2015-03-07 14:32:16

标签: wpf xaml polygon

如何使用Polygon在WPF中进行加号?

<Polygon Points="?,? ?,? ?,? ?,?" Fill="Green"></Polygon>

4 个答案:

答案 0 :(得分:3)

最简单的方法是使用Path

 <Path Stretch="Fill" Width="100" Height="100"
      Fill="Green"   
      Data="M4.1561281,2.2702953 L4.8524521,2.2702954 4.8509674,3.963097 5.8969377,3.9630803 5.8969378,5.0916036 4.8524628,5.1061913 4.8524521,6.7843885 4.1561281,6.7843887 4.1559771,5.0877741 3.1116421,5.0916036 3.1116421,3.9630803 4.1556735,3.9654722 4.1561281,2.2702953 z"/>

这个Path您可以使用&#34;直接选择&#34;在混合中轻松编辑。

多边形示例:

<Viewbox Width="50" Height="50">
    <Polygon Name="myPolygon" Fill="Green" ></Polygon>
</Viewbox>

代码隐藏:

myPolygon.Points = new PointCollection()
{
    new Point(4.1561279296875,2.27029538154602),
    new Point(4.85245227813721,2.27029538154602),
    new Point(4.85096740722656,3.9630970954895),
    new Point(5.89693784713745,3.96308040618896),
    new Point(5.89693784713745,5.09160375595093),
    new Point(4.85246276855469,5.10619115829468),
    new Point(4.85245227813721,6.78438854217529),
    new Point(4.1561279296875,6.78438854217529),
    new Point(4.15597724914551,5.0877742767334),
    new Point(3.11164212226868,5.09160375595093),
    new Point(3.11164212226868,3.96308040618896),
    new Point(4.15567350387573,3.96547222137451),
    new Point(4.1561279296875,2.27029538154602)
};

答案 1 :(得分:0)

我很可能不会使用Polygon而是使用两行:

    <Grid>
        <Line X1="0" Y1="50" X2="100" Y2="50" Stroke="Green" StrokeThickness="10"/>
        <Line X1="50" Y1="0" X2="50" Y2="100" Stroke="Green" StrokeThickness="10"/>
    </Grid>

答案 2 :(得分:0)

通常我们为“+”或“添加新项目按钮”等按钮创建“加号”。 这个案例有一个完整的代码示例:

按钮样式:

<Style TargetType="{x:Type ButtonBase}"
           x:Key="{x:Type ButtonBase}">
        <Setter Property="Background"
                Value="Transparent" />
        <Setter Property="BorderBrush"
                Value="Transparent" />
        <Setter Property="BorderThickness"
                Value="0" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <ContentPresenter Margin="{TemplateBinding Padding}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled"
                                 Value="False">
                            <Setter Property="Opacity"
                                    Value="0.5" />
                            <Setter Property="Foreground"
                                    Value="DimGray" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type Button}"
           BasedOn="{StaticResource {x:Type ButtonBase}}"
           x:Key="StyleButtonAddNewItem">
        <Setter Property="HorizontalContentAlignment"
                Value="Center" />
        <Setter Property="VerticalAlignment"
                Value="Center" />
        <Setter Property="Foreground"
                Value="Green" />
        <Setter Property="Padding"
                Value="2" />
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Path Data="M 8,00 12,00 12,8 12,8 20,8 20,12 12,12 12,20 8,20 8,12 0,12 0,8 8,8 Z"
                          Stretch="UniformToFill"
                          Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button}}}" />
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver"
                     Value="True">
                <Setter Property="Foreground"
                        Value="LimeGreen" />
            </Trigger>
            <Trigger Property="IsPressed"
                     Value="True">
                <Setter Property="Foreground"
                        Value="DarkGreen" />
            </Trigger>     <Button x:Name="PART_ButtonAddNewItem"
                                    VerticalAlignment="Top"
                                    Height="20"
                                    Width="20"
                                    Style="{StaticResource StyleButtonAddNewItem}" />
        </Style.Triggers>
    </Style>

使用此按钮:

 <Button x:Name="PART_ButtonAddNewItem"
                                    VerticalAlignment="Top"
                                    Height="20"
                                    Width="20"
                                    Style="{StaticResource StyleButtonAddNewItem}" />

答案 3 :(得分:0)

接受的答案对我没有用 - 没有显示任何内容。也许这是因为我的按钮上有自定义宽度..

要获取任何可调整大小的XAML图标,只需使用Modern UI Icons

即可

加号的特定解决方案是:

<Path Width="38" Height="38" Canvas.Left="19" Canvas.Top="19" Stretch="Fill" Fill="#FF000000" Data="F1 M 35,19L 41,19L 41,35L 57,35L 57,41L 41,41L 41,57L 35,57L 35,41L 19,41L 19,35L 35,35L 35,19 Z "/>

要调整图标大小,请更改高度和宽度,然后按比例更改Canvas.Left和Canvas.Top。

例如

<Button> <Path Width="19" Height="19" Canvas.Left="9.5" Canvas.Top="9.5" .../> </Button>