gpt4 book ai didi

wpf - 如何在WPF Grid中设置行边框和背景颜色

转载 作者:行者123 更新时间:2023-12-01 23:30:28 25 4
gpt4 key购买 nike

我们如何在WPF网格控件中设置边框和背景颜色,
我正在动态创建行和列,然后添加到网格中,
我们可以从后面的代码中设置颜色和边框吗?

最佳答案

这里有一些看起来效果很好的 hack。如果您将背景元素与通常放置在那里的元素一起放置在行/列中,它将充当背景。您只需要注意 XAML 中元素的顺序(元素以递增的 Z 顺序出现),或相应地设置 Panel.Zorder。

<Window x:Class="gridBackground.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Border Background="Red" />
<Border Grid.Row="2" Grid.Column="1" Background="Red" />
<Border Grid.Row="1" Background="LightBlue" />
<Border Grid.Row="2" Background="Orange" />
<Border Grid.Row="0" Grid.Column="1" Background="Orange" />
<TextBlock Grid.ColumnSpan="2" Grid.Row="1" Text="Here is some more text" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Grid.ColumnSpan="2" Text="Here is some text" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Grid.ColumnSpan="2" Grid.Row="2" Text="Here is even more text" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Window>

看起来像这样:

enter image description here

关于wpf - 如何在WPF Grid中设置行边框和背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11244457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com