我有一个 wpf mvvm 应用程序。 并且有一个包含多列的网格
在wpf中设置网格列背景颜色的最佳方法是什么?
dabble125 的答案很完美,但为了给您提供一个示例并提及一个注释,即放置矩形的位置很重要,请参阅代码:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!-- this is not a good place for text block.
the text block is beneath the rectangle
so it would not be seen -->
<!--<TextBlock Grid.Column="1" Text="Some Text"/>-->
<Rectangle Grid.Column="1" Grid.RowSpan="1000">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF83FF97" Offset="0" />
<GradientStop Color="White" Offset="1" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<TextBlock Grid.Column="1" Text="Some Text"/>
</Grid>
一种方法:
创建一个矩形并将其填充设置为您选择的颜色。
然后将其 Grid.RowSpan 值设置为一个较大的数字或您拥有的行数。
创建一个矩形并将其填充设置为您选择的颜色。
只有:
<Rectangle
Grid.Column="1"
Fill="#e8ebf1" />
对我有用。
之前答案的 Grid.RowSpan 实际上是没有用的,并且演示的 LinearGradientBrush 对于所要求的内容来说过于复杂。
无需将
RowSpan
值硬编码为固定数字,您可以使用 data binding
将值设置为其父网格 RowDefinitions.Count
属性
<Rectangle
Grid.Column="1"
Grid.RowSpan="{Binding RelativeSource={RelativeSource AncestorType=Grid},
Path=RowDefinitions.Count }"
Fill="DeepSkyBlue" />