开发者

Apply style from the database field in WPF

my problem is, i have different-different styles which are stored in database table. i will fetch this styles and store it into some string variable.now i want to apply this styles into my wpf controls. so how can i do that?

ex:

My Code is..

Window1.xaml
===========================

<Window x:Class="DynamicBindResourceDictionary.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microso开发者_开发问答ft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Button Margin="103,64,68,86" Name="button1" Content="Click Here!" Style="{DynamicResource baseStyle}"></Button>
    </Grid>
</Window>

Window1.xaml.cs
================

public partial class Window1 : Window
{ 
    //Store the style here from database
    string style = "<Style x:Key='baseStyle' TargetType='{x:Type Button}'>" +
    "<Setter Property='FontSize' Value='15' />" +
    "<Setter Property='Background' Value='Red' /></Style>";

    public Window1()
    {
        InitializeComponent();
    /* How to do that?  */
    }
}

if we cannot do using the string variable than what is the alternative solution for that..

pls help me

Dharmesh


Try this:

    public MainWindow()
    {
        string styleString = "<Style xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Key='baseStyle' TargetType='{x:Type Button}' >" +
                             "<Setter Property='FontSize' Value='15' />" +
                             "<Setter Property='Background' Value='Red' />"+
                             "<Setter Property='Height' Value='18' />" +
                             "</Style>";
        StringReader stringReader = new StringReader(styleString);
        XmlReader xmlReader = XmlReader.Create(stringReader);
        Style readerLoadStyle = (Style)XamlReader.Load(xmlReader);            
        ResourceDictionary rd = new ResourceDictionary();
        rd.Add("baseStyle", readerLoadStyle);
        Application.Current.Resources.MergedDictionaries.Add(rd);
        InitializeComponent();            
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜