WPF グリッド コントロールで境界線と背景色を設定するにはどうすればよいでしょうか。
行と列を動的に作成してグリッドに追加していますが、
コード ビハインドから色と境界線を設定できますか。
ベストアンサー1
うまく機能すると思われるちょっとしたハックを紹介します。行/列に、通常そこに配置する要素と一緒に背景要素を配置すると、背景として機能します。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>
次のようになります: