WPF资源
1.静态资源和动态资源:
静态资源是设置完成之后不会再发生变化的,动态资源是可以发生变化的:
例如:
<Window.Resources>
<SolidColorBrush x:Key="SolidColor" Color="Red"/>
</Window.Resources>
<Grid>
<StackPanel>
<Button Content="Update" Margin="10" Click="Button_Click"/>
<Button Content="Button1" Height="40" BorderThickness="3" BorderBrush="{StaticResource SolidColor}" Margin="10"/>
<Button Content="Button1" Height="40" BorderThickness="3" BorderBrush="{DynamicResource SolidColor}" Margin="10"/>
</StackPanel>
</Grid>
private void Button_Click(object sender, RoutedEventArgs e) { this.Resources["SolidColor"] = new SolidColorBrush(Colors.Black); }
当点击Update后,效果如下:

2.资源字典:
可以把资源放在一个地方,然后在App.xml添加一下,就可以变成公共的资源,在多个界面上使用;

加资源

在App.xaml将资源字典文件添加到集合当中:

当然,也可以在代码中获取资源:


浙公网安备 33010602011771号