首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

WPF,这种资源如何创建

2013-11-09 
WPF,这种资源怎么创建?类中的一个静态成员:namespace WPF2{class Brushs{public static SolidColorBrush b

WPF,这种资源怎么创建?
类中的一个静态成员:
namespace WPF2
{
    class Brushs
    {
        public static SolidColorBrush brush { get; set; }
        static Brushs()
        {
            brush = .......;   
        }
    }
}

请问,如何在XAML中创建一个SolidColorBrush资源,引用Brushs中的静态SolidColorBrush
<Window.Resources>
    <SolidColorBrush ........."/>
</Window.Resources>


该怎么写?

[解决办法]
http://www.cnblogs.com/lxy131/archive/2010/08/26/1808986.html
[解决办法]
你想要做什么?动态设定Resource?
这种情况下要用binding的。
[解决办法]


using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Media;

namespace WpfStaticResource
{
    class CustomBrush : INotifyPropertyChanged 
    {
        private SolidColorBrush _myBrush;

        public SolidColorBrush MyBrush
        {
            get
            {
                return _myBrush;
            }
            set
            {
                if (_myBrush != value)
                {
                    _myBrush = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}



<Window x:Class="WpfStaticResource.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfStaticResource"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">        
    <Window.Resources>
        <local:CustomBrush x:Key="data"/>
    </Window.Resources>
    <Window.DataContext>
        <Binding Source="{StaticResource data}" />
    </Window.DataContext>
    <StackPanel Height="150" Width="300">
        <TextBlock Background="{Binding Path=MyBrush}" Height="70"/>
        <Button Click="RedButton_Click">Red</Button>
        <Button Click="YellowButton_Click">Yellow</Button>
        <Button Click="GreenButton_Click">Green</Button>
    </StackPanel>


</Window>




using System.Windows;
using System.Windows.Media;

namespace WpfStaticResource
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ((CustomBrush)Resources["data"]).MyBrush = new SolidColorBrush(Colors.White);
        }

        private void RedButton_Click(object sender, RoutedEventArgs e)
        {
            ((CustomBrush)Resources["data"]).MyBrush = new SolidColorBrush(Colors.Red);
        }

        private void YellowButton_Click(object sender, RoutedEventArgs e)
        {
            ((CustomBrush)Resources["data"]).MyBrush = new SolidColorBrush(Colors.Yellow);
        }

        private void GreenButton_Click(object sender, RoutedEventArgs e)
        {
            ((CustomBrush)Resources["data"]).MyBrush = new SolidColorBrush(Colors.Green);
        }
    }
}

热点排行