Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

c# - 在C#中样式化列表框项目,而无需更改项目本身(Style listbox items in c# without changing the item itself)

I need to style a listbox item when creating it like so:

(我需要在创建列表框项目时设置其样式,如下所示:)

if (CheckOnline(item)) {
     listBox.Items.Add(item);
     listView.Items.Add(item);
} else
{
     listBox.Items.Add(item + "??");
     listView.Items.Add(item + "??");
}

So basically if CheckOnline returns false I want to modify the look of the item but not actually make the string item + "??".

(因此,基本上,如果CheckOnline返回false,我想修改项目的外观,但实际上不使字符串项目+“??”。)

Is there a way to make the item display differently from the actual content of that item?

(有没有办法使项目显示的内容不同于该项目的实际内容?)

I just need some way to signal to the user that the item can't be used at the moment.

(我只需要某种方式向用户发送信号,表明该商品目前无法使用。)

  ask by ZCT translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm assuming you're trying mock an E-commerce service.

(我假设您正在尝试模拟电子商务服务。)

So you need to grey out items which are not currently available.

(因此,您需要将当前不可用的项目显示为灰色。)

First you need to Have a collection of items in your ViewModel

(首先,您需要在ViewModel中有一组项目)

ObservableCollection<ItemObj> Items{get;set;}

And ItemObj should contain a property IsAvailable , which should be the result of CheckOnline() function.

(ItemObj应该包含属性IsAvailable ,该属性应该是CheckOnline()函数的结果。)

This collection should be bound to the ListBox/ListView you're using.

(此集合应绑定到您正在使用的ListBox / ListView。)

You can use a DataTrigger to write a custom style for Items which return false from CheckOnline()

(您可以使用DataTrigger为从CheckOnline()返回false的Items编写自定义样式)

<DataTrigger Binding="{Binding IsAvailable}" Value="False">
<!-- Write custom style here -->
</DataTrigger>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...