Line样式的TargetType在UWP中不起作用

时间:2019-12-20 01:58:59

标签: uwp uwp-xaml

我有以下代码:

<Grid>
    <Grid.Resources>
        <Style TargetType="Line">
            <Setter Property="Stroke" Value="Black"></Setter>
            <Setter Property="StrokeDashArray" Value="3"></Setter>
            <Setter Property="StrokeThickness" Value="3"></Setter>
        </Style>
    </Grid.Resources>
    <Line X1="20" X2="500" Y1="50" Y2="50"></Line>
    <Line X1="20" X2="500" Y1="100" Y2="100"></Line>
    <Line X1="20" X2="500" Y1="150" Y2="150"></Line>
</Grid>

enter image description here

您可能会看到屏幕截图,该样式的StrokeDashArray属性值仅位于第一行。为什么?

1 个答案:

答案 0 :(得分:1)

我可以重现此问题,但是我不确定是什么原因导致了这种意外行为。我将向团队询问此问题。解决方法是,您可以使用StaticResource共享StrokeDashArray的值。

<Grid>
    <Grid.Resources>
        <Style TargetType="Line">
            <Setter Property="Stroke" Value="Black"></Setter>
            <Setter Property="StrokeThickness" Value="3"></Setter>
        </Style>
        <x:String x:Key="strokeArray">3</x:String>
    </Grid.Resources>
    <Line X1="20" X2="500" Y1="50" Y2="50" StrokeDashArray="{StaticResource strokeArray}"></Line>
    <Line X1="20" X2="500" Y1="100" Y2="100" StrokeDashArray="{StaticResource strokeArray}"></Line>
    <Line X1="20" X2="500" Y1="150" Y2="150" StrokeDashArray="{StaticResource strokeArray}"></Line>
</Grid>