样式网格布局封面控件

时间:2018-02-20 16:52:52

标签: c# wpf xaml

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

<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>

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

这是它的样子:

enter image description here

这是我的窗口代码。

<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>

1 个答案:

答案 0 :(得分:0)

当您将样式全局应用于应用程序中的所有网格时,其他控件中使用的样式也会受到影响。例如,看看Window控件(来自vs设计师,左键单击窗口&gt;编辑模板&gt;编辑副本)

<Window.Resources>
    <ControlTemplate x:Key="WindowTemplateKey" TargetType="{x:Type Window}">
        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
            <Grid>
                <AdornerDecorator>
                    <ContentPresenter/>
                </AdornerDecorator>
                <ResizeGrip x:Name="WindowResizeGrip" HorizontalAlignment="Right" IsTabStop="false" Visibility="Collapsed" VerticalAlignment="Bottom"/>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="ResizeMode" Value="CanResizeWithGrip"/>
                    <Condition Property="WindowState" Value="Normal"/>
                </MultiTrigger.Conditions>
                <Setter Property="Visibility" TargetName="WindowResizeGrip" Value="Visible"/>
            </MultiTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
    <Style x:Key="WindowStyle1" TargetType="{x:Type Window}">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                        <AdornerDecorator>
                            <ContentPresenter/>
                        </AdornerDecorator>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="ResizeMode" Value="CanResizeWithGrip">
                <Setter Property="Template" Value="{StaticResource WindowTemplateKey}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

请注意GridBorder中定义的ControlTemplate

解决您的问题的一个简单方法是为您的样式指定一个键并手动将其指定给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>