本文介绍了ListView 克隆去掉默认字体功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几列的基本 ListView .

我使用以下方法创建一个 ListViewItem :

I create a ListViewItem using:

ListViewItem item = new ListViewItem();
item.Text = "First";
item.SubItems[1].Text = "Second";

然后将 ListViewItem 添加到我的 ListView

ListView1.Items.Add(item);

因此,我有一种方法可以更改 ListView1 Font ,如果 ListView 更改了字体,则所有 ListViewItem-s 也会执行同样的操作.

So, I have a method where I change the Font of the ListView1,and if the ListView changes the font, all ListViewItem-s do as well.

但是如果我使用 .Clone()函数

ListView1.Items.Add((ListViewItem)item.Clone());

它克隆 ListViewItem ,并对 Font 进行硬编码.

It clones the ListViewItem and it hard codes the Font.

因此,更改 ListView 字体不会更改 ListViewItem-s 字体.

So changing the ListView font won't change the ListViewItem-s font.

ListView1.Font = new Font("Microsoft Sans Serif", 15F, FontStyle.Regular);

推荐答案

只需处置 ListView 的当前 Font ,然后再设置新的一个:

Just Dispose the current Font of the ListView before setting a new one:

ListView1.Font.Dispose();
ListView1.Font = new Font("Microsoft Sans Serif", 15F, FontStyle.Regular);

这样,将强制 ListViewItem-s (包括克隆的代码)继承 ListView 控件的新字体.

This way, the ListViewItem-s including the cloned ones will be forced to inherit the new font of the ListView control.

这篇关于ListView 克隆去掉默认字体功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 22:31
查看更多