WPF 의 정적 자원 과 동적 자원

다음으로 이동:
http://hi.baidu.com/wangjunwangjuna/blog/item/c9dd3e4e9c1209dad1c86a91.html
먼저 정적 자원 과 동적 자원 의 예 시 를 살 펴 보 겠 습 니 다. 코드 는 다음 과 같 습 니 다.
XAML 코드 는 다음 과 같 습 니 다.
<Window x:Class="WPF      .Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <ImageBrush x:Key="MyBrush"
                    TileMode="Tile"
                    ViewportUnits="Absolute"
                    Viewport="0 0 30 30"
                    ImageSource="/image/1.png"
                    Opacity="0.9"/>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button Name="btn_Message" Grid.Row="0" Background="{StaticResource MyBrush}">1111111111</Button>
        <Button Grid.Row="1" Click="Button_Click">222222222222</Button>
        <Button Grid.Row="2" Background="{DynamicResource MyBrush}">33333333</Button>
    </Grid>
</Window>

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;
namespace WPF      
{
    /// <summary>
    /// Window1.xaml      
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Resources["MyBrush"] = new LinearGradientBrush(new GradientStopCollection() 
            { 
                new GradientStop()
                {
                    Color=Colors.Red,
                    Offset=0.1
                },
                new GradientStop()
                {
                    Color=Colors.Green,
                    Offset=0.5
                },
                new GradientStop()
                {
                    Color=Colors.Blue,
                    Offset=0.9
                }
            });
        }
    }
}

위의 예제 에서 볼 수 있 듯 이 두 번 째 단 추 를 누 르 면 자원 의 이미지 브러시 를 Linear 브러시 로 변경 합 니 다. 이때 동적 자원 은 이에 따라 바 뀌 고 정적 자원 은 바 뀌 지 않 습 니 다.

좋은 웹페이지 즐겨찾기