靜態資源和動態資源的區別在於靜態資源只從資源集合中獲取對象一次,然而動態資源在每次需要對象時都會重新從資源集合中查找對象。這意味着可以在同一鍵下放置一個全新對象,並且動態資源會應用該變化。
作為一般性規則,只有在下列情況下才需要使用動態屬性:
1.資源具有依賴系統設置的屬性。
2.計划通過編程替換動態資源。
不應該過渡使用動態資源。主要原因是對資源的修改未必會觸發對用戶界面的更新
<Window.Resources> <ImageBrush x:Key="TileBrush" x:Name="DynamicBrush" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 32 32" ImageSource="happyface.jpg" Opacity="0.3"></ImageBrush> </Window.Resources> <Grid> <StackPanel Margin="5"> <Button Background="{DynamicResource TileBrush}" Padding="5" FontWeight="Bold" FontSize="14" Margin="5"> 使用動態資源 </Button> <Button Padding="5" Margin="5" Click="cmdChange_Click" FontWeight="Bold" FontSize="14">改變畫刷</Button> <Button Background="{StaticResource TileBrush}" Padding="5" Margin="5" FontWeight="Bold" FontSize="14"> 使用靜態資源 </Button> </StackPanel> </Grid>
this.Resources["TileBrush"] = new SolidColorBrush(Colors.LightBlue);

