设置列背景在WPF的ListView / GridView的 [英] Setting Column Background in WPF ListView/Gridview
问题描述
我期待以设置列在WPF GridView控件的背景。许多谷歌结果指着设置GridViewColumn.CellTemplate更改列的外观。不过,我会见了设置背景颜色,当一个问题;它没有延伸到填充单元格:
I'm looking to set the background of a column in a WPF GridView. Many Google results have pointed towards setting the GridViewColumn.CellTemplate to change the appearance of a column. However, I'm met with an issue when setting the background color; it's not stretching to fill the cell:
下面是我的工作的XAML:
Here's the xaml I'm working with:
<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好措施:
And the xaml.cs for good measure:
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中的网格的宽度和高度比与阴性切缘可以产生一个接近结果的单元格大,但如果调整列,这个问题再次表明本身。
Setting the DataTemplate's Grid's width and height to be larger than the cell with a negative margin can produce a close result, but if you resize the column, the problem shows itself again.
<Grid Background="Yellow" Height="22" Width="50" Margin="-6">
有没有一种方法来填充颜色的单元格?
Is there a way to fill the cell with color?
推荐答案
将 HorizontalContentAlignment
的 ItemContainerStyle
:
<ListView ItemsSource="{Binding}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
结果:
这篇关于设置列背景在WPF的ListView / GridView的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!