標籤:style blog class code c java
方法一、定義在控制項內部
1 <Canvas Background="Red" Height="100" HorizontalAlignment="Left" Margin="90,80,0,0" Name="canvas1" VerticalAlignment="Top" Width="200" />
方法二、定義在控制項外部
1 <UserControl.Resources>2 <Style x:Key="StyleName" TargetType="Canvas"><!--x:Key是指樣式的名稱-->3 <Setter Property="Background" Value="Red"></Setter>4 </Style>5 </UserControl.Resources>6 <Grid x:Name="LayoutRoot" Background="White">7 <Canvas Style="{StaticResource StyleName}" Height="100" HorizontalAlignment="Left" Margin="90,80,0,0" Name="canvas1" VerticalAlignment="Top" Width="200" /><!--讓Canvas的樣式指定為靜態資源--> 8 </Grid>
要想讓外部樣式應用於所有相同類型的控制項,只要去掉資源中的樣式名稱,則TargetType指定的控制項都將應用該樣式
1 <UserControl.Resources>2 <Style TargetType="Canvas">3 <Setter Property="Background" Value="Red"></Setter>4 </Style>5 </UserControl.Resources>6 <Grid x:Name="LayoutRoot" Background="White">7 <Canvas Height="100" HorizontalAlignment="Left" Margin="90,80,0,0" Name="canvas1" VerticalAlignment="Top" Width="200" />8 <Canvas Height="100" HorizontalAlignment="Left" Margin="121,188,0,0" Name="canvas2" VerticalAlignment="Top" Width="200" />9 </Grid>
方法三、定義全域的樣式
將樣式放到App.xaml檔案的資源定義裡
1 <Application.Resources>2 <Style TargetType="Canvas"> 3 <Setter Property="Background" Value="Red"></Setter>4 </Style>5 </Application.Resources>