【问题标题】:Change Foreground item of ListView更改 ListView 的 Foreground 项
【发布时间】:2016-07-25 17:00:08
【问题描述】:

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

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

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

但是使用此代码item 始终为空。 任何帮助表示赞赏。

【问题讨论】:

    标签: c# xaml listview uwp


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的帮助。我得到:UWP15.exe 中发生了“System.NullReferenceException”类型的异常,但未在用户代码中处理其他信息:对象引用未设置为对象的实例。 in ListViewItem item = (ListViewItem)(ListView_Metrados_Body.ItemContainerGenerator.ContainerFromIndex(ListView_Metrados_Body.SelectedIndex));
    • @Julius 您应该从ListView 中选择一个项目,然后单击并运行此代码。
    • 是的,我正在这样做。我有一个按钮。
    • @Julius 我很确定这段代码可以正常工作,因为我在发布答案之前编译了它。可能您在其他地方遇到了一些问题。
    • 谢谢,我去查一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 2015-10-21
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 2014-03-07
    相关资源
    最近更新 更多