首页 > 代码库 > DevExpress 动态换肤

DevExpress 动态换肤

我们都知道Devexpress内置了很多themes,那要怎么在使用时动态更改呢。

下面是方法以:

1、如果你们已经有主题了,那就在XAML中删除类似下下面的语句。

dx:ThemeManager.ThemeName="LightGray" 

2、确保你的XAML中Window是引用下面的

<dx:DXWindow

    后台也一样:

MainWindow : DXWindow

3、下面就可以读取DevExpress中所有的主题:

comboBoxEdit1.ItemsSource = Theme.Themes;

4、更换应用主题:

ThemeManager.ApplicationThemeName = Theme.Themes[comboBoxEdit1.SelectedIndex].Name;

5、主程序更新布局:

this.UpdateLayout(); //重新布局

 

下面是Demo的完整代码:

<dx:DXWindow
    x:Class="theme.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
    Title="DXApplication" Height="300" Width="400"
    SnapsToDevicePixels="True" UseLayoutRounding="True"
    xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
    >
    <dx:DXWindow.Resources>

    </dx:DXWindow.Resources>

    <Grid>
        <dxe:ComboBoxEdit HorizontalAlignment="Left" Margin="64,85,0,0" Name="comboBoxEdit1" VerticalAlignment="Top" Width="150" />
        <dxe:TextEdit EditValue=http://www.mamicode.com/"一个中国人" Height="40"  Width="200" Margin="64,120,0,0" />
    </Grid>

</dx:DXWindow>
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.Shapes;
using DevExpress.Xpf.Core;


namespace theme
{
    public partial class MainWindow : DXWindow
    {
        public MainWindow()
        {
            InitializeComponent();           

            this.Loaded += delegate(object sender, RoutedEventArgs e)
            {
                comboBoxEdit1.ItemsSource = Theme.Themes;
                comboBoxEdit1.SelectedIndex = 0;
            };

            comboBoxEdit1.SelectedIndexChanged += delegate(object sender, RoutedEventArgs e)
            {
                ThemeManager.ApplicationThemeName = Theme.Themes[comboBoxEdit1.SelectedIndex].Name;
                this.UpdateLayout(); //重新布局
              };

        }
     }
}