如何更改所选项目(对象)中的列表框项目颜色?
问题描述:
我有下面的代码,我希望能做到以颜色从项目关联到该对象列表框项目更改:如何更改所选项目(对象)中的列表框项目颜色?
foreach (var item in lbMine.Items)
{
MyClass current = (MyClass)item;
if (current.ID == someParamSentID)
{
//How to change listbox color based on 'item'?
}
}
我在寻找,我没有改变XAML的解决方案。
感谢
答
你可能会通过调用ItemContainerGenerator.ContainerFromItem
方法包含当前项目的一个ListBoxItem:
if (current.ID == someParamSentID)
{
var lbItem = (ListBoxItem)lbMine.ItemContainerGenerator.ContainerFromItem(current);
...
}
通过获取一个ListBoxItem的'(ListBoxItem中)lbMine.ItemContainerGenerator.ContainerFromItem(电流)'。 – Clemens
谢谢,它运作良好,使它成为答案,所以我可以标记为已回答。谢谢 – RollRoll