【问题标题】:Setting Column Background in WPF ListView/Gridview在 WPF ListView/Gridview 中设置列​​背景
【发布时间】: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


    【解决方案1】:

    设置HorizontalContentAlignmentItemContainerStyle

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

    结果:

    【讨论】:

    • 这有助于不必设置宽度。但是,两列之间仍然存在差距。有什么办法可以关闭吗?
    • 唉,GridViewRowPresenter 将 Margin 硬编码为 6,0,6,0。恐怕要解决这个问题需要做大量的工作。
    • 一个 hacky 解决方法是将包含 Grid 的 Margin 设置为 -6,0,-6,0。
    • 我一直在尝试解决与列边距类似的问题 - 感谢您指出硬编码。现在,问题是为什么?猜猜我们永远不会知道。
    • 如果您需要一些列左对齐而其他列右对齐怎么办?该样式适用于所有这些。
    【解决方案2】:

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

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

    移动边距,使背景颜色填满整个单元格,但随后将它们移回,使文本仍然在正确的位置。暂时可以使用,直到正确修复为止。

    【讨论】:

    • 在挖掘这个已经挖掘的线程时;我很想知道这种现象现在是否“修复”了?在离开编码四年后,我正在尝试学习 WPF,并且似乎找不到任何更好的解决方案来创建不同颜色的无缝列背景。欢迎任何可以向我指出现代/更好方法的人:D
    猜你喜欢
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    相关资源
    最近更新 更多