简体   繁体   中英

Why is the style of DataGridCheckBoxColumn using MahApps.Metro not being applied? WPF

I'm launching the WPF window from class library. My XAML styles look like this:

<Controls:MetroWindow x:Class="AeonPlanter.UI.Window"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:AeonPlanter.UI"
         xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
         mc:Ignorable="d" d:DesignWidth="400" Background="White" Width="400" Height="447.333" Loaded="MetroWindow_Loaded" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Title="AeonPlanter" EnableDWMDropShadow="True">
<Controls:MetroWindow.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
            <!-- Accent and AppTheme setting -->
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Controls:MetroWindow.Resources>
<DataGrid x:Name="dtg_InventoryItems" HorizontalAlignment="Left" Margin="10,208,0,0" VerticalAlignment="Top" Width="360" Height="150" AutoGenerateColumns="False">

                <DataGrid.Columns>
                    <DataGridCheckBoxColumn Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=PlantItem, Mode=OneWay}" 
                                            ClipboardContentBinding="{x:Null}" 
                                            Header="Plant" 
                                            Width="58" 
                                            ElementStyle="{DynamicResource MetroDataGridCheckBox}" 
                                            EditingElementStyle="{DynamicResource MetroDataGridCheckBox}" CanUserReorder="False" CanUserResize="False" CanUserSort="False" />

                    <DataGridTextColumn Binding="{Binding ItemName}" CanUserResize="False" ClipboardContentBinding="{x:Null}" Header="Name" Width="*" IsReadOnly="True"/>
                    <DataGridTextColumn Binding="{Binding ItemId}" CanUserResize="False" ClipboardContentBinding="{x:Null}" Header="ID" IsReadOnly="True" Width="58"/>
                    <Controls:DataGridNumericUpDownColumn Binding="{Binding PlantAmount}" Header="Plant X" Width="70" CanUserReorder="False" CanUserResize="False" CanUserSort="False"/>
                </DataGrid.Columns>
            </DataGrid>

The problem now is that the checkbox column has default style and not Metro, despite in designer it shows it correctly.

I also added the following style to data grid:

<Style TargetType="DataGrid" BasedOn="{StaticResource {x:Type DataGrid}}" />

But no luck.

Any ideas why is that happening?

Use the {StaticResource} markup extension:

<DataGridCheckBoxColumn ...
                    ElementStyle="{StaticResource MetroDataGridCheckBox}" 
                    EditingElementStyle="{StaticResource MetroDataGridCheckBox}"

Or merge the resource dictionaries in your App.xaml file:

<Application x:Class="AeonPlanter.UI.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="Window.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
                <!-- Accent and AppTheme setting -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

</Application>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM