C# 样式网格布局覆盖控件

C# 样式网格布局覆盖控件,c#,wpf,xaml,C#,Wpf,Xaml,我正在尝试使用资源字典为项目中的所有网格布局设置背景色。这是我修改网格的文件代码 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Theme"> <SolidColorBrush x:Key=

我正在尝试使用资源字典为项目中的所有网格布局设置背景色。这是我修改网格的文件代码

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Theme">

<SolidColorBrush x:Key="GridBackColor" Color="Red"/>
<Style TargetType="Grid">
    <Setter Property="Background" Value="{StaticResource GridBackColor}"/>
    <Setter Property="Opacity" Value="0.5"/>
</Style> 
</ResourceDictionary>

设置背景属性后,网格上的所有控件都消失了,但当我设置不透明度时,我只能说所有控件都在网格布局下,任何鼠标事件都不起作用

下面是它的样子:

这是我的窗口代码

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="10*"/>
        <ColumnDefinition Width="125"/>
        <ColumnDefinition Width="20"/>
        <ColumnDefinition Width="125"/>
        <ColumnDefinition Width="10*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="10*"/>
        <RowDefinition Height="40"/>
        <RowDefinition Height="40"/>
        <RowDefinition Height="20"/>
        <RowDefinition Height="30"/>
        <RowDefinition Height="10*"/>
    </Grid.RowDefinitions>

    <Label Content="Name" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="3" FontSize="20"
           HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
    <TextBox Name="TbName" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="3" FontSize="20"
              HorizontalAlignment="Stretch" />

    <Button Content="Add" Name="BtAdd" Grid.Column="1" Grid.Row="4" IsDefault="True" 
            HorizontalAlignment="Right" VerticalAlignment="Center" Width="100" Click="BtAdd_Click"/>
    <Button Content="Close" Name="BtClose" Grid.Column="3" Grid.Row="4" IsCancel="True"
            HorizontalAlignment="Left" Width="100" Click="BtClose_Click"/>

</Grid>

将样式全局应用于应用程序中的所有网格时,其他控件中使用的网格也会受到影响。例如,查看
窗口
控件(在vs设计器中,左键单击窗口>编辑模板>编辑副本)

要使用它:

<Grid Style="{StaticResource GridStyle}">
...
</Grid>

...
<Style TargetType="Grid" x:Key="GridStyle">
    <Setter Property="Background" Value="{StaticResource GridBackColor}"/>
    <Setter Property="Opacity" Value="0.5"/>
</Style>
<Grid Style="{StaticResource GridStyle}">
...
</Grid>