我在我的大部分窗户从派生创建基本窗口类的过程。 显然,我们的最佳解决方案是一个单独的类,并适用于它的样式。
问题是, <Style ../>
我还没有被当它在应用App.Resources
。 也就是说,如果在外部的已定义ResourceDictionary
,并合并成App.xaml
的资源,或者本地词典和合并,或内联放入App.Resources
。 所述<Style ../>
是,然而,应用时,它被放置到Themes/Generic.xaml
。
这个问题可以在不脱离重写做什么特别的,在所有的基本窗口,除了证明DefaultStyleKeyProperty
。
下面是ThemeWindow
:
public class ThemeWindow : Window
{
static ThemeWindow()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ThemeWindow), new FrameworkPropertyMetadata(typeof(ThemeWindow)));
}
}
这里是非常简单<Style ../>
我想申请(它使Window
背景的红色,仅此而已):
<Style TargetType="{x:Type testing:ThemeWindow}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type testing:ThemeWindow}">
<Grid>
<Grid.Background>
<SolidColorBrush Color="Red"/>
</Grid.Background>
<AdornerDecorator>
<ContentPresenter />
</AdornerDecorator>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
在MainWindow
使用ThemeWindow
,简直就是下面的XAML代码:
<testing:ThemeWindow x:Class="Testing.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:testing="clr-namespace:Testing"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="125,83,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</testing:ThemeWindow>
现在,如前所述,如果你把那个Style
在它自己ResourceDictionary
的,包括它是这样的:
<App.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes/ThemeWindow.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</App.Resources>
.. 这是行不通的。 如果你直接内嵌样式到App.Resources
,这是行不通的。
我能找到它的工作的唯一情况就是调用ResourceDictionary
XAML Generic.xaml
,并将其放置到Themes/
应用程序的目录。
我想知道究竟为什么发生这种情况。
我唯一的理论是,当看到WPF控件类型,将头向Themes
,并扫描所有ResourceDictionary
S为类型,然后回落到Generic.xaml
并加载它。 这并不能解释为什么,如果它不能加载<Style />
可在合并ResourceDictionary
虽然。 注意如果它确实工作MergedDictionary
放入Generic.xaml
,原因显而易见。
我具有合并完全正常ResourceDictionary
到Generic.xaml
如果这是我必须做的。 我只是想在技术细节趴下,为什么它需要这样。
这不工作/工作的截图: