一个简单的窗口:
<Window x:Class="MyApp.MainWindow" xmlns="..." xmlns:x="...">
<Window.Resources>
<Style TargetType="Grid">
<Setter Property="Margin" Value="8"/>
</Style>
</Window.Resources>
<Grid>
<TextBox VerticalAlignment="Top" HorizontalAlignment="Left">Test</TextBox>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
它看起来像这样:

现在我们删除Window.Resources:
<Window x:Class="MyApp.MainWindow" xmlns="..." xmlns:x="...">
<Grid>
<TextBox VerticalAlignment="Top" HorizontalAlignment="Left">Test</TextBox>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
并将样式定义添加到App.xaml:
<Application x:Class="MyApp.App" xmlns="..." xmlns:x="..." StartupUri="View\MainWindow.xaml">
<Application.Resources>
<Style TargetType="Grid">
<Setter Property="Margin" Value="8"/>
</Style>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
奇怪的是,TextBox现在得到了一个填充:

为什么?