DataBinding WPF图像C#ListView SelectionChanged

时间:2017-02-23 07:21:15

标签: c# wpf listview data-binding

编辑:

问题: 我试图从ListBox中获取信息,如下所示: 当我点击listBox的一行时,我想将文件路径保存到字符串中以便以后使用它。我现在的问题是我不会使用我的代码获取文件路径,我认为问题是string filename = listView.SelectedItem.ToString();从listView,文件路径和状态(图像)中获取整个信息。

如何填写listView? 伪代码:

When Button is pressed
-> access xml-file (add filepath to a list)
-> deserelialize it
-> error checking
-> on error
---> add Statusimage to a list
-> update listView.Itemsource = ErrorLog 

(ErrorLog是我保存错误和状态图像的列表)

ErrorLog类:

class ErrorLog
{
    public string Status { get; set; }
    public string Log { get; set; }
}

如何从ListView获取Filepath?

private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string filename = listView.SelectedItem.ToString();
}  

希望此更新更好:)

dict

1 个答案:

答案 0 :(得分:0)

好的,我现在忽略了很多设计问题,只关注直接问题。

我认为listView.SelectedItem的实际类型为ErrorLogErrorLog.Log包含文件路径,除非所选项目是带有图片的状态消息。

var selectedItem = listView.SelectedItem as ErrorLog;
if (selectedItem != null &&
    string.IsNullOrWhitespace(selectedItem.Status) &&
    File.Exists(selectedItem.Log))
{
    string filename = selectedItem.Log;
}
// else it is a status log entry or something else doesn't match up