将xaml绑定到SelectedItemChanged上的ObservableCollection,返回null

时间:2015-08-03 13:03:27

标签: c# wpf xaml

我正在构建一个文件浏览器,我有一个绑定到ObservableCollection的ListView我想要的是当有人点击文件夹(左边是一个TreeView)时,它会填充Listview并填充正确的文件文本块中的信息。

我发现this帮助我到达了我的位置。但我仍然在文本块中返回null。 谢谢你的帮助!

我有private string start_Path

填充ListView的代码:

private void load_ListView(string path)
{
    var lv = File_List;
    lv.Items.Clear();
    var search_Directory = new DirectoryInfo(path);
    var item = new ListViewItem();
    try
    {

        foreach (var file in search_Directory.GetFiles())
        {

            lv.Items.Add(file);
        }
    }
    catch (Exception ex)
    {

    }

}
private void frm_File_Directory_Loaded(object sender, RoutedEventArgs e)
{
    ListDirectory(foldersItem, start_Path.ToString());
}

private void foldersItem_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    load_ListView( start_Path + "\\" + ((TreeViewItem)e.NewValue).Header.ToString());
    folder_Name = ((TreeViewItem)e.NewValue).Header.ToString();
    this.DataContext = File_Info_data.get_Files(start_Path + "\\" + ((TreeViewItem)e.NewValue).Header.ToString());            
}

的ObservableCollection:

   public static ObservableCollection<File_Information> get_Files(string path)
    {
        var temp = new ObservableCollection<File_Information>();
        File_Information file;
        FileInfo fileInfo = new FileInfo(path);
        try
        {
            file = new File_Information
            {
                file_Size = fileInfo.Length,
                date_Modified = fileInfo.LastWriteTime,
                file_Type = get_File_Type(fileInfo.Extension)
            };

            temp.Add(file);

            return temp;
        }
        catch (Exception ex) { }
        return null;
    }


    public static string get_File_Type(string extension)
    {
        string ext_Name = null;
        switch (extension)
        {
            case @"xlsx":
            case "xlsm":
            case "xls":
                ext_Name = "Excel File";
                break;           
            case "docx":
            case "docm":
            case "doc":
                ext_Name = "Word Document";
                break;
            case "pdf":
                ext_Name = "PDF Document";
                break;
            case "cad":
                ext_Name = "CAD File";
                break;
            case "DWG":
                ext_Name = "AutoCAD Drawing";
                break;
            case "jpg":
                ext_Name = "JPEG image";
                break;
            default:
                ext_Name = "Unknown File Type";
                break;
        }
        return ext_Name;
    }

XAML:

   <ListView.ItemTemplate>
        <DataTemplate>

            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Converter={StaticResource PathConverter}}"
                                Height="20"
                                Width="20"
                                Stretch="UniformToFill"
                                />
                <TextBlock x:Name="file_Name" Text="{Binding}" Width="300"></TextBlock>
                <TextBlock x:Name="Date_Modified" Text="{Binding date_Modified}" Width="200"></TextBlock>
                <TextBlock x:Name="File_Type" Text="{Binding file_Type}" Width="150"></TextBlock>
                <TextBlock x:Name="File_Size" Text="{Binding  file_Size}" Width="150"></TextBlock>
            </StackPanel>

        </DataTemplate>
    </ListView.ItemTemplate>


</ListView>

2 个答案:

答案 0 :(得分:0)

您可以改为将SelectedItem绑定到ViewModel中的某个媒体资源。

<ListView ItemsSource="{Binding SomeCollection}"
          SelectedItem="{Binding SelectedThing}"
          ...

您的视图模型将如下所示:

private Thing _SelectedThing;

public Thing SelectedThing
{ 
    get { return _SelectedThing; }
    set 
    {
        _SelectedThing = value;

        //Call a method and send whatever has been selected.
        DoSomethingUseful(value);

        //TODO: Notify property changed
    }
}

然后,您可以实施该方法:

private void DoSomethingUseful(Thing thing)
{  
    if (thing == null)
        return;

    //TODO: Whatever you need to do here.
}

使用此方法,每次选择更改时都会调用DoSomethingUseful方法。

答案 1 :(得分:0)

如果@MikeEason想法是正确的,那么一个简单的空检查将会:

private void foldersItem_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    if(e.NewValue == null)
        return;
    ... // rest of your code
}