내용 보기

작성자

관리자 (IP : 172.17.0.1)

날짜

2020-07-10 02:14

제목

[WPF] PresentationFramework.Aero 테마와 DevExpress 테마 동시 적용하기.


윈도우 테마를 클래식 모드로 사용 하는 윈도우[Windows Server 군]에서 윈도우7 Aero테마를 적용하기 위해 App.xaml에서 강제 적용 했을 경우 DevExpress테마의 일부가 적용 되지 않는 문제가 발생 한다.
이럴 경우 특정 컨트롤이 Loaded될때 해당 스타일을 무효화 시켜 DevExpress테마를 적용 시키면 된다.

app.xaml에서 PresentationFramework.Aero 테마 적용

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/PresentationFramework.Aero, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />
            </ResourceDictionary.MergedDictionaries>
 
            ...
            {My Style xaml Code}
 
        </ResourceDictionary>
</Application.Resources>
cs

ListView xaml

<ListView x:Name="xPolicyStateLogList" ItemsSource="{Binding PolicyStateLogList}" SelectedItem="{Binding SelectedPolicyStateLogInfo}" SelectionMode="Single" HorizontalContentAlignment="Stretch">
                        <ListView.ItemContainerStyle>
                            <Style TargetType="ListViewItem">
                                <EventSetter Event="MouseDoubleClick" Handler="PolicyStateLogListViewItem_DoubleClick" />
                            </Style>
                        </ListView.ItemContainerStyle>
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Width="90" Header="No">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding No}" TextAlignment="Center" TextTrimming="CharacterEllipsis" Style="{DynamicResource sTextBlock_Basic}"/>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                            </GridView>
                          </ListView.View>
</ListView>
cs

이 경우 ListView의 Scrollbar가 Aero테마로 적용 되어 DevExpress 테마가 적용 되지 않아 다음과 같은 헬퍼 클래스를 만들어 강제 적용 시켜 준다.

컨트롤의 ScrollBar에 DevExpress 테마 적용 헬퍼 클래스

using DevExpress.Mvvm.UI;
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls.Primitives;
 
namespace IntegratedManagementConsole.Commons
{
    /// <summary>
    /// 2015. 09. 07 엄태영
    /// DevExpress Theme 적용 Util 클래스
    /// Aero테마와 같은 별도의 다른 테마와 DevExpress테마와 같이 적용해서 쓰는 경우 특정 컨트롤에 DevExpress테마를 적용하는 Util 클래스
    /// </summary>
    public class DXThemeUtil
    {
        public static bool GetIsThemed(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsThemedProperty);
        }
 
        public static void SetIsThemed(DependencyObject obj, bool value)
        {
            obj.SetValue(IsThemedProperty, value);
        }
 
        // Using a DependencyProperty as the backing store for IsThemed. This enables animation, styling, binding, etc..
        public static readonly DependencyProperty IsThemedProperty =
            DependencyProperty.RegisterAttached("IsThemed"typeof(bool), typeof(DXThemeUtil), new PropertyMetadata(falsenew PropertyChangedCallback(OnChanged)));
 
        private static void OnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                FrameworkElement fe = d as FrameworkElement;
                fe.Loaded += fe_Loaded;
 
            }
        }
 
        static void fe_Loaded(object sender, RoutedEventArgs e)
        {
            FrameworkElement feParent = sender as FrameworkElement;
 
            var feList = LayoutTreeHelper.GetVisualChildren(feParent).OfType<ScrollBar>().ToList();
            foreach (FrameworkElement fe in feList)
            {
                fe.Style = null;
            }
        }
    }
}
cs

ListView에 테마 적용

<ListView x:Name="xErrorLogList" Util:DXThemeUtil.IsThemed="true" VirtualizingPanel.VirtualizationMode="Recycling" VirtualizingPanel.IsVirtualizing="True" ItemsSource="{Binding ErrorInfoViewModel.ErrorLogModelList}" SelectedItem="{Binding ErrorInfoViewModel.SelectedErrorLogInfo}" SelectionMode="Single" HorizontalContentAlignment="Stretch" Style="{DynamicResource sListView}">
                                            <ListView.ItemContainerStyle>
                                                <Style TargetType="ListViewItem">
                                                    <!--<EventSetter Event="MouseDoubleClick" Handler="ListViewItemDoubleClick" />-->
                                                </Style>
                                            </ListView.ItemContainerStyle>
                                            <ListView.View>
                                                <GridView>
                                                    <GridViewColumn Width="30">
                                                        <GridViewColumn.Header>
                                                            <CheckBox x:Name="ErrorInfo_AllCheck" Click="ErrorInfo_AllCheck_Click" />
                                                        </GridViewColumn.Header>
                                                        <GridViewColumn.CellTemplate>
                                                            <DataTemplate>
                                                                <CheckBox IsChecked="{Binding IsSelectCheck}" />
                                                            </DataTemplate>
                                                        </GridViewColumn.CellTemplate>
                                                    </GridViewColumn>
 
                                                    <GridViewColumn Width="90" Header="번호">
                                                        <GridViewColumn.CellTemplate>
                                                            <DataTemplate>
                                                                <TextBlock Text="{Binding No}" TextAlignment="Center" TextTrimming="CharacterEllipsis" Style="{DynamicResource sTextBlock_Basic}"/>
                                                            </DataTemplate>
                                                        </GridViewColumn.CellTemplate>
                                                    </GridViewColumn>
 
                                                    <GridViewColumn Width="120" Header="시작 일시">
                                                        <GridViewColumn.CellTemplate>
                                                            <DataTemplate>
                                                                <TextBlock Text="{Binding StartDateTime, StringFormat=d}" TextAlignment="Center" TextTrimming="CharacterEllipsis" Style="{DynamicResource sTextBlock_Basic}"/>
                                                            </DataTemplate>
                                                        </GridViewColumn.CellTemplate>
                                                    </GridViewColumn>
 
                                                    <GridViewColumn Width="120" Header="종료 일시">
                                                        <GridViewColumn.CellTemplate>
                                                            <DataTemplate>
                                                                <TextBlock Text="{Binding EndDateTime, StringFormat=d}" TextAlignment="Center" TextTrimming="CharacterEllipsis" Style="{DynamicResource sTextBlock_Basic}"/>
                                                            </DataTemplate>
                                                        </GridViewColumn.CellTemplate>
                                                    </GridViewColumn>
 
                                                    <GridViewColumn Width="120" Header="오류 유형">
                                                        <GridViewColumn.CellTemplate>
                                                            <DataTemplate>
                                                                <TextBlock Text="{Binding ErrorType}" TextAlignment="Center" TextTrimming="CharacterEllipsis" Style="{DynamicResource sTextBlock_Basic}"/>
                                                            </DataTemplate>
                                                        </GridViewColumn.CellTemplate>
                                                    </GridViewColumn>
 
                                                    <GridViewColumn Width="120" Header="출발지 IP">
                                                        <GridViewColumn.CellTemplate>
                                                            <DataTemplate>
                                                                <TextBlock Text="{Binding OriginIPAddress}" TextAlignment="Center" TextTrimming="CharacterEllipsis" Style="{DynamicResource sTextBlock_Basic}"/>
                                                            </DataTemplate>
                                                        </GridViewColumn.CellTemplate>
                                                    </GridViewColumn>
 
                                                    <GridViewColumn Width="120" Header="목적지 IP">
                                                        <GridViewColumn.CellTemplate>
                                                            <DataTemplate>
                                                                <TextBlock Text="{Binding DestinationIPAddress}" TextAlignment="Center" TextTrimming="CharacterEllipsis" Style="{DynamicResource sTextBlock_Basic}"/>
                                                            </DataTemplate>
                                                        </GridViewColumn.CellTemplate>
                                                    </GridViewColumn>
 
                                                    <GridViewColumn Width="150" Header="정책명">
                                                        <GridViewColumn.CellTemplate>
                                                            <DataTemplate>
                                                                <TextBlock Text="{Binding PolicyName}" TextAlignment="Center" TextTrimming="CharacterEllipsis" Style="{DynamicResource sTextBlock_Basic}"/>
                                                            </DataTemplate>
                                                        </GridViewColumn.CellTemplate>
                                                    </GridViewColumn>
                                                </GridView>
                                            </ListView.View>
</ListView>
cs

다음 처럼 app.xaml에 Style을 이용하여 모든 컨트롤에 일괄 적용 시킬 수 도 있다.

app.xaml에 Style을 적용하여 모든 ScrollBar의 스타일을 무효화하여 DevExpress Theme Style 적용

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/PresentationFramework.Aero, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />
            </ResourceDictionary.MergedDictionaries>
 
            <!--PresentationFramework.Aero Theme가 DevExpress Theme를 오버로드 하는 문제로 인해 해당 컨트롤 스타일 무료화-->
            <!--해당 컨트롤은 DevExpress Theme Style로 적용 된다.-->
            <Style TargetType="ScrollBar" />
            <!--PresentationFramework.Aero Theme가 DevExpress Theme를 오버로드 하는 문제로 인해 해당 컨트롤 스타일 무료화 End-->
 
 
            ...
            {My Style xaml Code}
 
        </ResourceDictionary>
</Application.Resources>
cs


출처1

출처2