如何在 WPF 中使 XAML 加号?
How to make a Plus sign in XAML in WPF?
如何使用多边形在 WPF 中制作加号?
<Polygon Points="?,? ?,? ?,? ?,?" Fill="Green"></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>
最简单的方法是使用 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"/>
您可以使用 "Direct Selection" 在 Blend 中轻松编辑此 Path
。
多边形示例:
<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)
};
通常我们为“+”或"add new item button"等按钮创建"plus signs"。
这种情况下有一个完整的代码示例:
按钮样式:
<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}" />
接受的答案对我不起作用 - 没有显示任何内容。也许这是因为我的按钮上有自定义宽度..
要获得任何可调整大小的 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>
如何使用多边形在 WPF 中制作加号?
<Polygon Points="?,? ?,? ?,? ?,?" Fill="Green"></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>
最简单的方法是使用 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"/>
您可以使用 "Direct Selection" 在 Blend 中轻松编辑此 Path
。
多边形示例:
<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)
};
通常我们为“+”或"add new item button"等按钮创建"plus signs"。 这种情况下有一个完整的代码示例:
按钮样式:
<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}" />
接受的答案对我不起作用 - 没有显示任何内容。也许这是因为我的按钮上有自定义宽度..
要获得任何可调整大小的 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>