【发布时间】:2009-09-04 15:18:03
【问题描述】:
我希望在 WPF GridView 中设置列的背景。许多 Google 结果都指向设置 GridViewColumn.CellTemplate 以更改列的外观。但是,我在设置背景颜色时遇到了问题;它不会拉伸以填充单元格:
这是我正在使用的 xaml:
<Window x:Class="ScratchPadWpf.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Width="300" Height="300">
<Grid>
<ListView ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid Background="Red">
<TextBlock Text="{Binding FirstName}"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid Background="Yellow">
<TextBlock Text="{Binding LastName}"/>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
还有 xaml.cs 是很好的衡量标准:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DataContext = new[]
{
new {FirstName = "Jim", LastName = "Bob"},
new {FirstName = "Frank", LastName = "Smith"},
new {FirstName = "Tooth", LastName = "Paste"},
};
}
}
将 DataTemplate 的 Grid 的宽度和高度设置为大于具有负边距的单元格可以产生关闭结果,但如果调整列的大小,问题再次出现。
<Grid Background="Yellow" Height="22" Width="50" Margin="-6">
有没有办法用颜色填充单元格?
【问题讨论】:
标签: wpf xaml listview gridview styles