【问题标题】:WPF Change Grid and Grid Border opacity without affecting childrenWPF 更改网格和网格边框不透明度而不影响子级
【发布时间】:2012-01-29 17:33:32
【问题描述】:

我有一个网格,我想让不透明度为 0.5。 我也有这个网格的边框,以使其圆角,我希望这个边框也有不透明度 0.5。 我想要这一切而不影响网格的内容。 我成功地改变了网格的不透明度而不影响内容:

        <Grid  Grid.Column="0"  Grid.Row="0" Margin="10,15,5,5" >
        <Border BorderThickness="7" CornerRadius="4" >
            <Grid>
                <Grid.Background>
                    <SolidColorBrush Color="#73B2F5" Opacity="0.5"/>
                </Grid.Background>
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="1*"/>
                </Grid.RowDefinitions>
                <Button Grid.Row="1" Grid.Column="0" Width="40" Height="40" Content="Asaf"></Button>

            </Grid>
        </Border>
    </Grid>

在上面的示例中,当网格为 0.5 时,按钮具有完全不透明度,但边框呢? 如何使边框的不透明度为 0.5,而不影响其中的网格和网格内容(所有边框子项)?

我试过了,但它不起作用:

        <Grid  Grid.Column="0"  Grid.Row="0" Margin="10,15,5,5" >
        <Border BorderThickness="7" CornerRadius="4">
            <Border.Background>
                <SolidColorBrush Color="#73B2F5" Opacity="0.5"/>
            </Border.Background>
            <Grid>
                <Grid.Background>
                    <SolidColorBrush Color="#73B2F5" Opacity="0.5"/>
                </Grid.Background>
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="1*"/>
                </Grid.RowDefinitions>
                <Button Grid.Row="1" Grid.Column="0" Width="40" Height="40" Content="Asaf"></Button>

            </Grid>
        </Border>
    </Grid>

【问题讨论】:

    标签: wpf grid border opacity


    【解决方案1】:

    只需将边框的 BorderBrush-Property(不是 Background-Property)设置为:

    <Border.BorderBrush>
       <SolidColorBrush Color="#000000" Opacity="0.5"/>
    </Border.BorderBrush>
    

    在您的示例中,它看起来像这样:

    <Grid  Grid.Column="0"  Grid.Row="0" Margin="10,15,5,5" >
        <Border BorderThickness="7" CornerRadius="4">
            <Border.BorderBrush>
               <SolidColorBrush Color="#000000" Opacity="0.5"/>
            </Border.BorderBrush>
            <Grid>
                <Grid.Background>
                    <SolidColorBrush Color="#73B2F5" Opacity="0.5"/>
                </Grid.Background>
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="1*"/>
                </Grid.RowDefinitions>
                <Button Grid.Row="1" Grid.Column="0" Width="40" Height="40" Content="Asaf"></Button>
    
            </Grid>
        </Border>
    </Grid>
    

    【讨论】:

      【解决方案2】:

      对于那些想要更改DataGrid 中网格线的边框/不透明度的人,可以简单地在资源中设置不透明颜色:

       <Window.Resources>
           <SolidColorBrush x:Key="StackOverflowGray" Color="LightGray" Opacity=".3" />
       </Window.Resources>
      

      ...

      那么以下用法将在数据网格中显示较浅的单元格边框:

      <DataGrid GridLinesVisibility="All"
                HorizontalGridLinesBrush="{StaticResource StackOverflowGray}"
                VerticalGridLinesBrush="{StaticResource StackOverflowGray}"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-20
        • 2014-01-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多