4-WPF资源-2资源层次

This commit is contained in:
Bunny 2025-01-12 16:20:47 +08:00
parent d5a368e206
commit f7e968e7e6
2 changed files with 52 additions and 15 deletions

View File

@ -3,10 +3,45 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:_4_WPF资源_2资源层次"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
Icon="public/003540AH4M72.jpg" Title="_4_WPF资源_2资源层次" Height="450" Width="800">
<Window.Resources>
</Window.Resources>
<StackPanel>
<Button Margin="5" Padding="15 5">Button</Button>
</Grid>
</Window>
<Button Margin="5" Padding="15 5" FontWeight="Bold" FontSize="14">
<Button.Resources>
<ImageBrush x:Key="MyImageBrush" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 10 10"
ImageSource="public/003540AH4M72.jpg" Opacity="0.3" />
</Button.Resources>
<Button.Background>
<StaticResource ResourceKey="MyImageBrush" />
</Button.Background>
按钮
</Button>
<Button Background="{DynamicResource BunnyImageBrush}" Padding="5" Margin="5" FontWeight="Bold" FontSize="14">
<Button.Resources>
<ImageBrush x:Key="BunnyImageBrush" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 32 32"
ImageSource="public/003540AH4M72.jpg" Opacity="0.3" />
</Button.Resources>
<Button.Content>按钮</Button.Content>
</Button>
<Button Background="{DynamicResource BunnyImageBrush}" Padding="5" Margin="5" FontWeight="Bold" FontSize="14">
<Button.Resources>
<ImageBrush x:Key="BunnyImageBrush" x:Shared="False" TileMode="Tile" ViewportUnits="Absolute"
Viewport="0 0 32 32"
ImageSource="public/003540AH4M72.jpg" Opacity="0.3" />
</Button.Resources>
<Button.Content>x:Shared="False" 是否共享资源</Button.Content>
</Button>
<Button Name="FindResource" Click="FindResource_OnClick">查找资源</Button>
</StackPanel>
</Window>

View File

@ -1,18 +1,10 @@
using System.Text;
using System.Windows;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace _4_WPF资源_2资源层次;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
@ -20,4 +12,14 @@ public partial class MainWindow : Window
{
InitializeComponent();
}
private void FindResource_OnClick(object sender, RoutedEventArgs e)
{
var button = (Button)sender;
var tryFindResource = button.TryFindResource("BunnyImageBrush");
Console.WriteLine($"尝试查找资源找不到返回null{tryFindResource}");
var findResource = button.FindResource("BunnyImageBrush");
Console.WriteLine($"尝试查找资源,找不到报错:{findResource}");
}
}