在 WPF ListView/Gridview 中设置列​​背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1379902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:57:32  来源:igfitidea点击:

Setting Column Background in WPF ListView/Gridview

wpfxamllistviewgridviewstyles

提问by statenjason

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:

我希望在 WPF GridView 中设置列​​的背景。许多 Google 结果都指向设置 GridViewColumn.CellTemplate 以更改列的外观。但是,我在设置背景颜色时遇到了一个问题;它不会拉伸以填充单元格:

Ugly Grid View

丑陋的网格视图

Here's the xaml I'm working with:

这是我正在使用的 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>

And the xaml.cs for good measure:

和 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"},
    };
  }
}

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.

将 DataTemplate 的 Grid 的宽度和高度设置为大于具有负边距的单元格可以产生接近的结果,但是如果您调整列的大小,问题又会再次出现。

<Grid Background="Yellow" Height="22" Width="50" Margin="-6">

Still Ugly

还是丑

Is there a way to fill the cell with color?

有没有办法用颜色填充单元格?

回答by Kent Boogaart

Set the HorizontalContentAlignmentof the ItemContainerStyle:

设置HorizontalContentAlignmentItemContainerStyle

<ListView ItemsSource="{Binding}">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

Result:

结果:

alt text

替代文字

回答by mike

Digging up an old thread, but I found a dodgy fix for this

挖掘一个旧线程,但我找到了一个狡猾的解决方法

<Grid Background="{Binding backGround}" Margin="-6,0,-6,0">
  <TextBlock Margin="6,0,6,0" Text="{Binding myText}" TextAlignment="Right" />
</Grid>

Moves the margins so the background color fills the entire cell, but then moves them back so the text is still in the right spot. Works for now until it's fixed properly.

移动边距使背景颜色填满整个单元格,然后将它们移回使文本仍然在正确的位置。暂时有效,直到它被正确修复。