静态资源和动态资源
静态资源:有动作也不会改变原本的状态(StaticResource SolidColor)
动态资源:可以根据相应的动作来变换姿态(DynamicResource SolidColor)
单击按钮实现按钮边框颜色变化
<Window.Resources>
<SolidColorBrush x:Key="add"
Color=" red" />
</Window.Resources>
<Grid>
<StackPanel>
<Button Content="sb1"
BorderBrush="{StaticResource add}"
Click="Button_Click"
Height="50"
Width="118"
BorderThickness="5">
<Button.Background>
<ImageBrush ImageSource="/th.jpg" />
</Button.Background>
</Button>
<Button Content="sb2"
BorderBrush="{StaticResource add}"
Height="52"
Width="120"
BorderThickness="5">
<Button.Background>
<ImageBrush ImageSource="/R-C.jpg" />
</Button.Background>
</Button>
<Button Content="sb3"
BorderBrush="{DynamicResource add}"
Height="56"
Width="124"
BorderThickness="5">
<Button.Background>
<ImageBrush ImageSource="/微信图片_20220320014343.png" />
</Button.Background>
</Button>
</StackPanel>
</Grid>
namespace 项目练习
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Resources["add"] = new SolidColorBrush(Colors.Green);
}
}
}
按下之后边框变颜色了 就是因为第三个使用的动态资源引用 所以能变化 则第二个静态没有变化
资源字典
资源字典:资源字典中的资源要添加到界面当中,在App里面添加
创建资源字典
然后要在App.xaml里添加我们的资源,然后才能在外面引用
然后就可以在任意窗口引用资源了 按下F10 在style可以查看可引用的资源
namespace 项目练习
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Resources["add"] = new SolidColorBrush(Colors.Green);
var solidColor = App.Current.FindResource("add");
var style = App.Current.FindResource("Buttonstyle");
}
}
}
效果和前面的一样