在WPF中画十字

问题描述 投票:0回答:6

我有一个 WPF 控件。

我需要在背景中有一个十字架,像这样:
enter image description here

之后,我就可以在“交叉”背景上添加其他控件: enter image description here

知道当我调整控件大小时,十字应该遵循其大小,我应该如何绘制十字?

.net wpf wpf-controls
6个回答
14
投票

快速而肮脏的方法是使用线条并将其坐标绑定到某些父容器的宽度和高度。像这样的东西:

<Grid Name="parent">
    <Line  X1="0" Y1="0" X2="{Binding ElementName='parent', Path='ActualWidth'}" Y2="{Binding ElementName='parent', Path='ActualHeight'}" 
           Stroke="Black" StrokeThickness="4" />
    <Line  X1="0" Y1="{Binding ElementName='parent', Path='ActualHeight'}" X2="{Binding ElementName='parent', Path='ActualWidth'}" Y2="0" Stroke="Black" StrokeThickness="4" />
</Grid>

使用网格作为父级意味着在行之后添加到网格的任何其他子级都将显示在行的顶部:

<Grid Name="parent">
    <Line  X1="0" Y1="0" X2="{Binding ElementName='parent', Path='ActualWidth'}" Y2="{Binding ElementName='parent', Path='ActualHeight'}" 
           Stroke="Black" StrokeThickness="4" />
    <Line  X1="0" Y1="{Binding ElementName='parent', Path='ActualHeight'}" X2="{Binding ElementName='parent', Path='ActualWidth'}" Y2="0" Stroke="Black" StrokeThickness="4" />
    <Label Background="Red" VerticalAlignment="Center" HorizontalAlignment="Center">My Label</Label>
</Grid>

10
投票

您可以使用单个

Path
项目,而不是为布局引擎创建两个元素:

<Path Data="M0,0L16,16M16,0L0,16" Stroke="Black" />

这是一个 16x16“X”符号:

您可以在

Data
元素中指定不同的大小,或者根据需要使用
ViewBox
将其缩放到其父级大小。

请注意,线条末端是方形的,并稍微超出 16x16 方形:

如果您希望框恰好为 16x16,您可以在

Width="16" Height="16"
上设置
Path
,这会剪辑线端:


8
投票

解决此问题的另一种方法是将所有内容放入

Viewbox
中并使用
Stretch="fill"
。它将为您处理重新调整大小,同时保持适当的比例。在这种情况下,您不需要使用数据绑定。

<Grid  HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" >
    <Viewbox HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" Stretch="Fill">
        <Grid>
            <Line  X1="0" Y1="0" X2="100" Y2="100" Stroke="Black" StrokeThickness="1" />
            <Line  X1="0" Y1="100" X2="100" Y2="0" Stroke="Black" StrokeThickness="1" />
        </Grid>
    </Viewbox>
    <Label Background="Red" VerticalAlignment="Center" HorizontalAlignment="Center">My Label</Label>
</Grid>

3
投票

Matt Burland 的答案让我的应用程序不断闪烁(因为我认为对“父级”的引用调整了它的大小......然后调整了线条的大小,等等......)

所以我使用 Stretch=Fill 并抑制对“父级”的引用。现在工作得很好。

<Line x:Name="Line1" Visibility="Hidden" Stroke="Red" StrokeThickness="2" Stretch="Fill"
                X1="0" Y1="0" X2="1" Y2="1" />
<Line x:Name="Line2" Visibility="Hidden" Stroke="Red" StrokeThickness="2" Stretch="Fill"
                X1="0" Y1="1" X2="1" Y2="0" />

这是这个解决方案和这个one

的混合

1
投票
    <Line  X1="10" Y1="10" X2="50" Y2="50" Stroke="Black" StrokeThickness="4" />
    <Line  X1="10" Y1="50" X2="50" Y2="10" Stroke="Black" StrokeThickness="4" />

如果您想知道 x 和 y 值来自哪里,只需绘制笛卡尔坐标并插入即可

line 1 - point 1:(10,10), point 2:(50,50)
line 2 - point 1:(10,50), point 2:(50,10)

参考形状 http://msdn.microsoft.com/en-us/library/ms747393.aspx

将标签放在 XAML 中的 Line 元素之后/下方,这将使其绘制在线条上


0
投票

使用 StreamGeometry 并将其创建为代码隐藏。 此处的示例展示了如何在代码中创建 StreamGeometry。

流中的“M”在代码中将是“BeginFigure”。然后还有 LineTo、ArcTo 等。创建一个流,将其分配给 Path,您就可以生成代码,而无需转换为字符串然后再次转换回来。

© www.soinside.com 2019 - 2024. All rights reserved.