c# - 如果在应用程序资源中设置,为什么网格背景颜色会覆盖整个窗口?
问题描述
我想知道为什么在应用程序资源中设置网格的背景颜色会导致整个窗口被网格背景覆盖,即使我没有在 XAML 主窗口文件中指定网格面板。
MainWindow.xaml:
<Window x:Class="TicTacToe.DesktopApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Tic-tac-toe"
Height="420"
Width="420"
ResizeMode="NoResize"
WindowStyle="SingleBorderWindow">
<DockPanel>
<Button Content="Button"></Button>
</DockPanel>
</Window>
应用程序.xaml:
<Application x:Class="TicTacToe.DesktopApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="Button">
<Setter Property="Margin" Value="10" />
</Style>
<Style TargetType="Grid">
<Setter Property="Background" Value="Red" />
<!--Uncomment the line below to see that button seems to be hidden under the grid.-->
<!--<Setter Property="Opacity" Value="0.5" />-->
</Style>
</Application.Resources>
</Application>
MainWindow.xaml.cs
并且App.xaml.cs
只包含自动生成的代码。没什么特别的。
Visual Studio 预览按预期显示窗口:
而不是它,我得到:
问题
为什么它会这样?是否有某个隐藏且始终存在的网格覆盖整个窗口并包含在我的样式规则中?如果是这样,为什么会这样做以及为什么会以可观察到的一秒钟的延迟来应用它?
解决方案
这是可视化树设计工具在调试时用于选择可视化树中的元素的网格。您可以使用事件设置器验证这一点,然后单击网格,或者通过运行应用程序,而不是在调试模式下。
<Style TargetType="Grid">
<Setter Property="Background" Value="Red" />
<EventSetter Event="PreviewMouseDown" Handler="Grid_PreviewMouseDown"/>
<!--Uncomment the line below to see that button seems to be hidden under the grid.-->
<!--<Setter Property="Opacity" Value="0.5" />-->
</Style>
,
public partial class App : Application
{
private void Grid_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
MessageBox.Show(VisualTreeHelper.GetParent(sender as Grid).ToString());
}
}
推荐阅读
- salt-stack - 如何获取要在其上运行盐的 salt-minion 的 FQDN
- typescript - 异步函数的返回类型必须是全局 Promise
类型 - c# - 如何使用 EF6 获取 SQL Server 列大小
- google-chrome - chrome中ctrl+e和ctrl+k的区别
- javascript - 我如何获取两个本地日期时间之间的确切差异,并且时间应该像秒表一样每秒更新一次?
- java - 将 Firestore 与 JavaFX Maven 应用程序集成时出错(IntelliJ Idea)
- c - 无符号位域值与有符号值的比较
- python - Python NumPy - 3D 数组的角度切片
- django - 在序列化程序和表单中重命名模型的字段 - Django
- java - MapsActivity.java 不适用于高于 6.0 的 android 版本