[英]Why is the style of DataGridCheckBoxColumn using MahApps.Metro not being applied? WPF
I'm launching the WPF window from class library. 我正在从类库启动WPF窗口。 My XAML styles look like this: 我的XAML样式如下所示:
<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. 现在的问题是,尽管在设计器中它正确显示,但复选框列具有默认样式而不是Metro。
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: 使用{StaticResource}
标记扩展名:
<DataGridCheckBoxColumn ...
ElementStyle="{StaticResource MetroDataGridCheckBox}"
EditingElementStyle="{StaticResource MetroDataGridCheckBox}"
Or merge the resource dictionaries in your App.xaml
file: 或在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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.