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后,效果如下:

image

 2.资源字典:

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

image

 加资源

image

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

image

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

image

 

posted @ 2026-05-21 11:37  你好呀嗯嗯  阅读(12)  评论(0)    收藏  举报