WPF基础之资源
2021-06-22 04:04
标签:找不到 art free 列表 nfx 多个 content sources 根据 WPF基础之资源 标签:找不到 art free 列表 nfx 多个 content sources 根据 原文地址:https://www.cnblogs.com/lovezhangyu/p/10218298.html WPF资源
资源集合
Window.Resources>
ImageBrush x:Key="TitleBrush" TileMode="Tile" ViewboxUnits="Absolute" Viewport="0 0 32 32"ImageSource="" Opacity="0.3">ImageBrush>
Window.Resources>
StackPanel>
Button Background="{StaticResource TitleBrush}">RequeryButton>
StackPanel>
资源的层次
Window.Resources>
ImageBrush x:Key="TitleBrush" TileMode="Tile" ViewboxUnits="Absolute" Viewport="0 0 32 32"
ImageSource="happyface.jpg" Opacity="0.3">ImageBrush>
Window.Resources>
StackPanel>
Button Background="{StaticResource TitleBrush}">RequeryButton>
Button Background="{DynamicResource TitleBrush}">
Button.Resources>
ImageBrush x:Key="TitleBrush" TileMode="Tile" ViewboxUnits="Absolute" Viewport="0 0 32 32"
ImageSource="abc.jpg" Opacity="0.3">ImageBrush>
Button.Resources>
Button.Content>Another Titled ButtonButton.Content>
Button>
StackPanel>
静态资源和动态资源
ImageBrush brush=(ImageBrush)this.Resources["TitleBrush"];
brush.Viewport = new Rect(0,0,5,5);
this.Resources["TitleBrush"] = new SolidColorBrush(Colors.LightBlue);
非共享资源
ImageBrush x:Key="TitleBrush" x:Shared="False" TileMode="Tile" ViewboxUnits="Absolute" Viewport="0 0 32 32"
ImageSource="happyface.jpg" Opacity="0.3">ImageBrush>
通过代码访问资源
private void txt_TextChanged(object sender, RoutedEventArgs e)
{
Button cmd = (Button)sender;
ImageBrush brush = (ImageBrush)cmd.FindResource("TitleBrush");
}
应用程序资源
Application x:Class="HelloWpf.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="GridDemoWindow.xaml">
Application.Resources>
ImageBrush x:Key="TitleBrush" TileMode="Tile" ViewboxUnits="Absolute" Viewport="0 0 32 32"
ImageSource="happyface.jpg" Opacity="0.3">ImageBrush>
Application.Resources>
Application>
系统资源
资源字典
ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
ImageBrush x:Key="TitleBrush" TileMode="Tile" ViewboxUnits="Absolute" Viewport="0 0 32 32"
ImageSource="happyface.jpg" Opacity="0.3">ImageBrush>
ResourceDictionary>
Application x:Class="HelloWpf.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="GridDemoWindow.xaml">
Application.Resources>
ResourceDictionary>
ResourceDictionary.MergedDictionaries>
ResourceDictionary Source="AppBrushs.xaml">ResourceDictionary>
ResourceDictionary.MergedDictionaries>
ResourceDictionary>
Application.Resources>
Application>