简体   繁体   English

在 WPF 中,如何强调 ListView 的顶行?

[英]In WPF, how do I emphasize the top row of a ListView?

I have a listview in WPF that currently looks like this:我在 WPF 中有一个列表视图,目前看起来像这样:

当前列表视图

and is generated from this code:并由此代码生成:

<ListView>
  <ListView.View>
    <GridView>
      <GridViewColumn DisplayMemberBinding="{Binding Author}" Header="Author"/>
      <GridViewColumn DisplayMemberBinding="{Binding MessageShort}" Header="Message"/>
    </GridView>
  </ListView.View>
</ListView>

I would like to visually emphasize or distinguish the first item (the Unsaved Changes line in the picture).我想在视觉上强调或区分第一项(图中未保存的更改行)。 This is not a header, but the item to emphasize will always be the first.这不是 header,但要强调的项目始终是第一个。 Is it possible to do so cleanly in WPF, whether from XAML or code?是否可以在 WPF 中干净地做到这一点,无论是来自 XAML 还是代码?

My research has showed things like using Triggers and/or ItemTemplateSelectors , but both seem like way too much when the only logic is "is the first element".我的研究显示了诸如使用 Triggers 和/或ItemTemplateSelectors之类的东西,但是当唯一的逻辑是“是第一个元素”时,两者似乎都太多了。 I also found AlternationIndex and AlternationCount and could conceivably set the AlternationCount to some number high enough to never be used and give different templates to AlternationIndex 1 and the rest, though this feels like a hack and still non-trivial.我还找到了AlternationIndexAlternationCount ,并且可以想象将 AlternationCount 设置为足够高的数字,以使其永远不会被使用,并为 AlternationIndex 1 和 rest 提供不同的模板,尽管这感觉像是一个 hack 并且仍然不平凡。 Not to mention I'm not quite sure how to implement it and to make sure it doesn't interfere with the GridView.更不用说我不太确定如何实现它并确保它不会干扰 GridView。

Changing style or template properties for a fixed position doesn't seem like it should be a difficult thing to do, but I can't seem to figure it out.更改固定 position 的样式或模板属性似乎并不难,但我似乎无法弄清楚。 Am I missing something obvious?我错过了一些明显的东西吗?

Binding to PreviousData should do the trick:绑定到 PreviousData 应该可以解决问题:

        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource PreviousData}}" Value="{x:Null}">
                        <Setter Property="FontWeight" Value="Bold"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListView.ItemContainerStyle>

Easiest thing for you would probably be to just add a Brush property to your class that you can bind too.对您来说最简单的事情可能就是将Brush属性添加到您也可以绑定的 class 中。 You can set this property value based on your requirements.您可以根据需要设置此属性值。

Note: Brush requires reference to System.Windows.Media注意: Brush需要参考System.Windows.Media

In your class add.在您的 class 添加。

//Adjust getter/setter if using iNotifyPropertyChanged
public Brush MyBrush { get; set; } = Brushes.White;

Now bind the ListView row background to the property.现在将ListView行背景绑定到该属性。

<ListView Name="listView" >
   <ListView.Resources>
        <Style TargetType="{x:Type ListViewItem}">
           <Setter Property="Background" Value="{Binding MyBrush}"/>
        </Style>
   </ListView.Resources>
   <ListView.View>
        <GridView>
          <GridViewColumn DisplayMemberBinding="{Binding Author}" Header="Author"/>
             <GridViewColumn DisplayMemberBinding="{Binding MessageShort}" Header="Message"/>
        </GridView>
   </ListView.View>
</ListView>        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM