wpf 如何设置 ListView 项目的样式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4474436/
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 22:24:36  来源:igfitidea点击:

How do I Style ListView Items?

wpflistview

提问by Jeff LaFay

I have a ListViewthat is data bound and I want to alter the font properties for each item. I'm not having much luck finding the appropriate properties. The ListViewis pretty simple so I don't expect it to be too difficult to change, I'm just not finding what I'm wanting as of yet.

我有一个ListView数据绑定,我想更改每个项目的字体属性。我没有太多运气找到合适的属性。这ListView很简单,所以我不认为它很难改变,我只是还没有找到我想要的东西。

<ListView ItemsSource="{Binding Updates}">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding TimeStamp}" Header="TimeStamp" />
            <GridViewColumn DisplayMemberBinding="{Binding UpdateData}" />
        </GridView>
    </ListView.View>
</ListView>

回答by Thomas Levesque

You can set the ItemContainerStyle:

您可以设置ItemContainerStyle

<ListView ItemsSource="{Binding Updates}">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding TimeStamp}" Header="TimeStamp" />
            <GridViewColumn DisplayMemberBinding="{Binding UpdateData}" />
        </GridView>
    </ListView.View>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="FontSize" Value="14" />
            <Setter Property="Foreground" Value="Blue" />
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="FontStyle" Value="Italic" />
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

Note that it will apply to the itemsof the ListView, not the ListViewitself (e.g. the column headers won't be affected). If you want to apply those properties to the whole ListView, you can set them directly on the ListView:

请注意,它将应用于 的项目,而ListView不是其ListView本身(例如,列标题不会受到影响)。如果要将这些属性应用于整个ListView,可以直接在 ListView 上设置它们:

<ListView ItemsSource="{Binding Updates}"
          Foreground="Blue" FontSize="14" ...>