내 목표는 simpel이지만 알아낼 수는 없습니다. Titel에 대한 미안하지만 더 나은 설명을 생각해 낼 수 없었습니다 ...WPF 가운데 맞춤 텍스트가 늘어난 라벨로 고정 폭
나는 현재 시간을 표시하는 레이블 (1 초 간격의 타이머에 연결됨)이있는 usercontrol을가집니다. 라벨은 부모의 너비이며 텍스트는 중앙에 정렬됩니다. 형식은 DateTime.ToString ("HH : mm : ss")이며, FontFamily 및 크기는 사용자가 조정할 수 있습니다. 지금까지 이상한 것은 없었습니다 ... 그러나 텍스트가 중앙에 정렬되어 있으므로 시간이 12:34:02 12:34:11과 다른 픽셀 너비라고 할 수 있습니다. (물론 글꼴에 따라 다름) 이로 인해 레이블 점프가 발생합니다 (자동 중심이기 때문에).
아래 코드는 그 예입니다. 캔버스가 그 위에 물건을 그리는 데 사용되며 뷰 박스가 사용되어 부모에서 자동 크기 조정됩니다.
코드 :
<Grid>
<Viewbox>
<Canvas Name="canv" Height="300" Width="300">
<StackPanel Name="stckpnlDateTime">
<Label Name="lblDateOrText"
Grid.Column="0"
Grid.Row="0"
Content = "------"
FontSize="25"
Foreground="GhostWhite"
HorizontalContentAlignment="Center"
VerticalAlignment="Bottom"
FontFamily="Arial"
Width="Auto"/>
<Label Name="lblTime"
Grid.Column="0"
Grid.Row="1"
Content = "-- : -- : --"
FontSize="25"
Foreground="GhostWhite"
HorizontalContentAlignment="Center"
VerticalAlignment="Top"
FontFamily="DS-Digital"
Width="Auto"/>
</StackPanel>
</Canvas>
</Viewbox>
</Grid>
Private Sub SystemTime_Tick(sender As Object, e As EventArgs) Handles tmrSystemTime.Tick
lblTime.Content = Now.ToString("HH : mm : ss")
End Sub
그래서 난 다른 접근 방식, 10 열 및 8 개 라벨, 각 문자에 대한 하나 큰 그리드를 시도하고, 부모 (셀)에 레이블을 스트레칭. 이것은 작동하고 문자를 고정 된 위치에 유지합니다. 그러나 마지막 열의 너비는 나머지보다 작습니다 ...이 이미지에서 모자를 볼 수 있습니다. 두 번째 보라색 열은 내 뜻입니다.
Example alignment
코드 :
<UserControl.Resources>
<Style x:Key="LabelStyle" TargetType="Label">
<Setter Property="Foreground" Value="White" />
<Setter Property="FontFamily" Value="DS-Digital" />
<Setter Property="FontSize" Value="40"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Right"/>
<Setter Property="Background" Value="Green" />
</Style>
</UserControl.Resources>
<Grid HorizontalAlignment="Stretch">
<Viewbox HorizontalAlignment="Stretch">
<Canvas Name="canv" Height="300" Width="300" HorizontalAlignment="Stretch">
<StackPanel Name="stckpnlDateTime" HorizontalAlignment="Stretch">
<Label Name="lblDateOrText"
Grid.Column="0"
Grid.Row="0"
Content = ""
FontSize="25"
Foreground="GhostWhite"
HorizontalContentAlignment="Center"
VerticalAlignment="Bottom"
FontFamily="Arial"
Width="Auto"/>
<Grid Name="GridTimeLabel" HorizontalAlignment="Stretch" Width="Auto" Grid.Column="0" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Label Background="Purple" Grid.Column="0" Grid.Row="0"/>
<Label Name="lblTime1" Grid.Column="1" Grid.Row="0" Style="{StaticResource LabelStyle}" Content="-"/>
<Label Name="lblTime2" Grid.Column="2" Grid.Row="0" Style="{StaticResource LabelStyle}" Content="-"/>
<Label Name="lblTime3" Grid.Column="3" Grid.Row="0" Style="{StaticResource LabelStyle}" Content=":"/>
<Label Name="lblTime4" Grid.Column="4" Grid.Row="0" Style="{StaticResource LabelStyle}" Content="-"/>
<Label Name="lblTime5" Grid.Column="5" Grid.Row="0" Style="{StaticResource LabelStyle}" Content="-"/>
<Label Name="lblTime6" Grid.Column="6" Grid.Row="0" Style="{StaticResource LabelStyle}" Content=":"/>
<Label Name="lblTime7" Grid.Column="7" Grid.Row="0" Style="{StaticResource LabelStyle}" Content="-"/>
<Label Name="lblTime8" Grid.Column="8" Grid.Row="0" Style="{StaticResource LabelStyle}" Content="-"/>
<Label Background="Purple" Grid.Column="9" Grid.Row="0"/>
</Grid>
</StackPanel>
</Canvas>
</Viewbox>
</Grid>
길고도 짧은 이야기, 내가 STUK 해요 .... 희망 누군가가 올바른 방향으로 날 지점 수 있습니다.
고정 폭 글꼴을 사용할 수 없습니까? – Jai
그는 폰트가 사용자에 의해 조정될 수 있다고 말합니다 – GenericTeaCup
속도가 너무 빠르다고 생각합니다 ... – Jai