wpf资源基础
# 动态资源和静态资源
<Window.Resources>
<SolidColorBrush x:Key="SolidColor" Color="Red" />
</Window.Resources>
<Grid>
<StackPanel>
<Button Margin="10" Content="update" Click="Button_Click"></Button>
<Button Margin="10" Content="button1" BorderBrush="{StaticResource SolidColor}"></Button>
<Button Margin="10" Content="button2" BorderBrush="{DynamicResource SolidColor}"></Button>
</StackPanel>
</Grid>
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
this.Resources["SolidColor"] = new SolidColorBrush(Colors.Blue);
1
StaticResource
: 引入静态资源,目标发生改变不会跟着变DynamicResource
: 引入动态资源,目标发证改变会跟着变
# 资源字典
右键添加
-> 资源字典
-> ButtonStyle.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="DefualtButtonStyle" TargetType="Button">
<Setter Property="Foreground" Value="Blue" />
<Setter Property="FontSize" Value="15"/>
</Style>
</ResourceDictionary>
1
2
3
4
5
6
7
2
3
4
5
6
7
全局引用App.xaml
<Application.Resources>
<!-- 资源字典 -->
<ResourceDictionary>
<!-- 字典集合 -->
<ResourceDictionary.MergedDictionaries>
<!-- 追加地点 -->
<ResourceDictionary Source="ButtonStyle.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
布局中直接使用即可
<Button Margin="10" Style="{StaticResource DefualtButtonStyle}" Content="button1" />
1
查找字典
var style = App.Current.FindResouce("DefualtButtonStyle");
1
上次更新: 2023/12/12, 21:55:41