WPF中使用资源字典文件

<Window x:Class="WPFDemo.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>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary
                  Source="Resources/MyResourceDictionary.xaml">
                </ResourceDictionary>
                <ResourceDictionary
                  Source="Resources/OthersStyle.xaml">
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <Image Source="/WPFDemo;component/Images/AddResourceDictionary.jpg"></Image>
    </Grid>
</Window>

 

public partial class Window1 : Window
{
    private ResourceDictionary myresourcedictionary;
    private ResourceDictionary mystyles;

    public Window1()
    {
        InitializeComponent();
       
        myresourcedictionary = new ResourceDictionary();

        myresourcedictionary.Source =
            new Uri("/WPFDemo;component/Resources/MyResourceDictionary.xaml",
                UriKind.RelativeOrAbsolute);

        mystyles = new ResourceDictionary();

        mystyles.Source = new Uri("/WPFDemo;component/Resources/OthersStyle.xaml",
                UriKind.RelativeOrAbsolute);

    }

    public void ApplyStyle()
    {
        Style mybuttonstyle = mystyles["MyStyle"] as Style;
        Button mybutton = new Button();
        mybutton.Style = mybuttonstyle;
    }
}

posted @ 2012-01-15 10:58  zhh  阅读(705)  评论(0编辑  收藏  举报