问题
I have the following code:
<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>
You could see the screenshoot, the StrokeDashArray
property value in the style only appled to the first line. Why?
回答1:
I could reproduce this issue, but I'm not sure what caused this unexpected behavior. I'll ask the team about this issue. As a workaround, you could use StaticResource for sharing the value of 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>
来源:https://stackoverflow.com/questions/59419033/the-targettype-for-line-in-style-doesnt-work-in-uwp