【问题标题】:Binding xaml to ObservableCollection on SelectedItemChanged returning null将 xaml 绑定到 SelectedItemChanged 上的 ObservableCollection 返回 null
【发布时间】:2015-08-03 21:20:23
【问题描述】:

我正在构建一个文件资源管理器,我有一个绑定到 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>

【问题讨论】:

  • 什么和在哪里返回null
  • View-model 中的集合属性在哪里?
  • 我的文件名出现了,其余为空

标签: c# wpf xaml


【解决方案1】:

您可以改为将 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 方法。

【讨论】:

  • 这是更多的 MVVM 方法,但它并没有改变任何事情,只是强调null 检查,这使得它(null 检查)成为可能的答案。
【解决方案2】:

如果@MikeEason 的想法是正确的,那么一个简单的空检查就可以了:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多