ListView绑定到ObservableCollection<添加到属性时不更新

本文关键字:属性 更新 绑定 ObservableCollection ListView 添加 | 更新日期: 2023-09-27 18:09:41

我正在构建一个文件资源管理器,当用户双击一个TreeView时,它会触发此事件。它填充了一个ObservableCollection<T>,绑定到我的ListView称为File_List。FillCollection方法填充FileList与正确的信息。

我已经把它从c#填充改为绑定,因为我认为它更容易维护。谢谢你的宝贵时间。有这个告诉我执行INotifyPropertyChanged,我有RaisedPropertyChanged方法。

private void folderItems_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    try
    {
        TreeViewItem item = (TreeViewItem)folderItems.SelectedItem;
        FileAttributes attr = File.GetAttributes(item.Tag.ToString());
        if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
        {
            _ViewModel.FileList = FillFileModel.FillCollection(item.Tag.ToString());
        }
        else
        {
           //Do something else
        }
    }
    catch (Exception) { }
}

FillFileModel Class

public static ObservableCollection<Files> FillCollection(string Path)
{
    ObservableCollection<Files> files = new ObservableCollection<Files>();
    var searchDirectory = new DirectoryInfo(Path);
    try
    {
        Files getFile;
        foreach (var file in searchDirectory.GetFiles())
        {
            getFile = new Files
            {
                FileName = file.Name,
                FileSize = file.Length.ToString("#,##0 KB"),
                DateModified = file.LastWriteTime,
                FileType = getFileType(file.FullName)
            };
            files.Add(getFile);
        }
    }
    catch (Exception)
    {
    }
    return files;
}
public ObservableCollection<Files> FileList
{
    get { return _FileList; }
    set
    {
        if (_FileList!=value)
        {
            _FileList = value;
            RaisedPropertyChanged("FileList");
        }
    }
}

RaisedPropertyChanged

public event PropertyChangedEventHandler PropertyChanged;
protected void RaisedPropertyChanged(string propertyName)
{
    PropertyChangedEventHandler handler = this.PropertyChanged;
    if (handler != null)
    {
        PropertyChangedEventArgs args = new PropertyChangedEventArgs(propertyName);
        handler(this, args);
    }
}

**XAML:**
<Window.Resources>
    <uc:ucNav x:Key="Nav"/>        
    <uc:FileExplorerViewModel x:Key="viewModel"/>
    <local:FileImageConverter x:Key="ImageConverter"/>
</Window.Resources>
 <ListView Width="580" Margin="205,32,9,10" x:Name="File_List" ItemsSource="{Binding Path=FileList}">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="150" Header="Name" >
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <Image Source="{Binding Converter={StaticResource ImageConverter}}" Height="20" Width="20" Stretch="Fill"/>
                            <TextBlock x:Name="file_Name" Text="{Binding FileName}"/>
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="150">
                <GridViewColumnHeader>
                    <TextBlock Text="Date Modified" />
                </GridViewColumnHeader>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock x:Name="Date_Modified"  Text="{Binding DateModified}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="150">
                <GridViewColumnHeader>
                    <TextBlock Text="File Type" />
                </GridViewColumnHeader>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock x:Name="File_Type" Text="{Binding FileType}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Width="130">
                <GridViewColumnHeader>
                    <TextBlock Text="File Size" />
                </GridViewColumnHeader>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock x:Name="File_Size" Text="{Binding FileSize}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

ListView绑定到ObservableCollection<添加到属性时不更新

听起来你的DataContext没有正确设置,因为你发布的其他代码看起来是正确的。

在点击处理程序中设置一个断点,并仔细检查File_List.DataContext == _ViewModel

DataContext通常是从父控件继承的,或者如果这是最上面的窗口,它可以从代码后面设置。在极少数情况下,您也可以绑定或设置该值,但通常不建议这样做,因为这会导致混淆和调试问题。