WPF—ResourceDictionary

作者:追风剑情 发布于:2019-8-16 17:40 分类:C#

一、新建资源字典文件

333.png

444.png

Dictionary1.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfTest"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">

    <sys:String x:Key="key001">资源字典string</sys:String>
    <Color x:Key="colorkey001">#FF3232</Color>
    <SolidColorBrush x:Key="solidBrush001" Color="{StaticResource colorkey001}"/>
    <SolidColorBrush x:Key="solidBrush002" Color="#FF707070"/>

    <BitmapImage x:Key="menu_item_normal">
        <BitmapImage.UriSource>
            Resources\Image\menu_item_normal.png
        </BitmapImage.UriSource>
    </BitmapImage>

    <BitmapImage x:Key="menu_item_over">
        <BitmapImage.UriSource>
            Resources\Image\menu_item_over.png
        </BitmapImage.UriSource>
    </BitmapImage>
</ResourceDictionary>

App.xaml

<Application x:Class="WpfTest.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfTest"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary x:Name="dic1" Source="Dictionary1.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

MainWindow.xaml

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        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:WpfTest"
        xmlns:Properties="clr-namespace:WpfTest.Properties" x:Class="WpfTest.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Background="{StaticResource solidBrush001}" Content="Button" HorizontalAlignment="Left" Margin="20,26.213,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
        <TextBlock x:Name="textBlock" Background="{StaticResource solidBrush002}" Text="{StaticResource key001}"  HorizontalAlignment="Left" Margin="114.8,30.213,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="115.2"/>
        <Image x:Name="img" Width="100" Source="{StaticResource menu_item_normal}" Margin="20,60,673.6,330.413"/>
    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.ComponentModel;//INotifyPropertyChanged

namespace WpfTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            BitmapImage menu_item_normal = this.FindResource("menu_item_over") as BitmapImage;
            this.img.Source = menu_item_normal;
        }
    }
}

运行测试

1111.png2222.png

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号