c# - 更改 ListView 的前景项目

标签 c# xaml listview uwp

我想更改 UWP 的 ListView 上项目的前景。 我正在使用:

int i_DeleteRow = ListView1.SelectedIndex;
var item = ListView1.Items[i_DeleteRow] as ListViewItem;

if (item != null)
{
    item.Foreground = new SolidColorBrush(Colors.Red);
}

但是使用此代码item始终为空。 如有任何帮助,我们将不胜感激。

最佳答案

您需要使用ItemContainerGenerator.ContainerFromIndex 。它返回一个 DependencyObject,然后您可以将其转换为 ListBoxItem 并使用 ListBoxItem 的属性,例如 Foreground:

ListViewItem item = (ListViewItem)(ListView1.ItemContainerGenerator.ContainerFromIndex(ListView1.SelectedIndex));
if (item != null)
{
    item.Foreground = new SolidColorBrush(Colors.Red);
}

关于c# - 更改 ListView 的前景项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38573581/

相关文章:

c# - Wator/GameOfLife 如何检查控制台项目中的现有单元格?

c# - 多维数组符号之间的区别(object[][] 和 object[,])

c# - 如何将用户控件复选框绑定(bind)到列表框中

xaml - 使用ScrollViewer进行图像缩放时,可以保持向左平移

android - 选择所有行并取消选择 ListView 中的所有行选项

c# - 当它在您的机器上运行良好时,您如何确定崩溃的原因?

c# - 监控 Windows 中的常规键盘快捷键(例如按两次 Ctrl 调出 Google 桌面搜索)?

xaml - 如何在 BindableLayout.ItemsSource 中绑定(bind)项目的索引

javascript - kendo ui mobile listview 无尽滚动只显示一个元素

Android:在带有姓名和号码的 ListView 中设置联系人照片