为什么没有应用使用MahApps.Metro的DataGridCheckBoxColumn的样式? WPF

时间:2017-06-29 12:18:41

标签: c# wpf user-interface

我从类库中启动WPF窗口。我的XAML样式如下所示:

<\w+(?=[ >]) # Begin matching opening tags
[^<>]*>      # Up to end
|            # Or (then)
(:)          # Any remaining colons

现在的问题是复选框列具有默认样式而不是Metro,尽管在设计器中它显示正确。

我还在数据网格中添加了以下样式:

<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>

但没有运气。

任何想法为什么会发生这种情况?

1 个答案:

答案 0 :(得分:1)

使用{StaticResource}标记扩展程序:

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

或合并App.xaml文件中的资源词典:

<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>