我尝试使用XAML和C#创建WPF软件。该软件使用大量具有相似外观/样式的TextBox和Labels。
例如,在以下代码中,我的所有标签都共享相同的HorizontalAlignment,VerticalAlignment,Margin和FontSize。如何指定类或通用样式以应用于所有元素?换句话说,我正在寻找类似于CSS中的类和ID的东西。
<Label Grid.Column="1" Grid.Row="4" Content="Project Name:" HorizontalAlignment="Stretch" Margin="0" VerticalAlignment="Center" FontSize="14"/>
<TextBox x:Name="ProjectNameInput" Grid.Column="2" Grid.Row="4" HorizontalAlignment="Stretch" VerticalAlignment="Center" Height="23" TextWrapping="Wrap" Text="" Margin="5,1"/>
<Label Grid.Column="1" Grid.Row="6" Content="Activity Name:" HorizontalAlignment="Stretch" Margin="0" VerticalAlignment="Center" FontSize="14"/>
<TextBox x:Name="ActivityNameInput" Grid.Column="2" Grid.Row="6" HorizontalAlignment="Stretch" VerticalAlignment="Center" Height="23" TextWrapping="Wrap" Text="" Margin="5,1"/>
<Label Grid.Column="1" Grid.Row="8" Content="Address:" HorizontalAlignment="Stretch" Margin="0" VerticalAlignment="Center" FontSize="14"/>
<TextBox x:Name="AddressInput" Grid.Column="2" Grid.Row="8" Grid.ColumnSpan="3" HorizontalAlignment="Stretch" VerticalAlignment="Center" Height="23" TextWrapping="Wrap" Text="" Margin="5,1"/>
<Label Grid.Column="1" Grid.Row="10" Content="Latitude:" HorizontalAlignment="Stretch" Margin="0" VerticalAlignment="Center" FontSize="14"/>
<TextBox x:Name="LatitudeInput" Grid.Column="2" Grid.Row="10" HorizontalAlignment="Stretch" VerticalAlignment="Center" Height="23" TextWrapping="Wrap" Text="" Margin="5,1"/>
答案 0 :(得分:1)
您需要了解WPF中的样式。谷歌“wpf styles”是一个很好的教程的例子http://www.wpftutorial.net/styles.html。
StackOverflow的另一个好问题是Good study material for WPF Styles。