使用INotifyPropertyChanged在WPF中绑定系统相关信息

时间:2016-07-09 16:36:52

标签: c# wpf xaml mvvm inotifypropertychanged

我希望使用 INotifyPropertyChanged WPF XAML中使用MVVM Binding 列出 Temp文件

视图模型是 TempViewModel.cs

class TempViewModel : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    public List<FileInfo> CacheFiles
    {
        get
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
            System.IO.DirectoryInfo di = new DirectoryInfo(path);
            return di.GetFiles().ToList();
        }
    }
}

Temp文件夹中的文件可能会不时变化。每当使用XAML UI中的 INotifyPropertyChanged 在Temp文件夹中更新文件时,我都需要自动更新

我怎么能在XMAL中绑定它?

MainWindow.xaml

<Window x:Class="Binding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox Name="LstProduct" ItemsSource="{Binding CacheFiles}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=FullName}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        this.DataContext = new TempViewModel();
    }
}

我无法在UI中获取更新的FileName列表。请在临时文件夹中添加或删除文件的时候,如何更新用户界面?

3 个答案:

答案 0 :(得分:1)

使用FileSystemWatchersee the docs for how to use it)并订阅其中一个活动。在该处理程序中,使用PropertyChanged调用"CacheFiles"事件,以通知UI重新获取CacheFiles属性。

您当然可以使用ObservableCollection进行改进并手动添加/删除条目,但我所描述的方式应该可行。

答案 1 :(得分:1)

您应该使用FileSystemWatcher和Changed event。例如,当FileSystemWatcher引发Changed事件时,事件处理程序应该通过引发PropertyChanged来更改CacheFile属性

答案 2 :(得分:1)

答案是 FileSystemWatcher

您应修改 TempViewModel.cs

class Cleaner : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    private List<KeyValuePair<string, ulong>> _memoryPool = new List<KeyValuePair<string, ulong>>();
    public List<KeyValuePair<string, ulong>> MemoryPool
    {
        get { return _memoryPool; }
        set
        {
            _memoryPool = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("MemoryPool"));
        }
    }


    private void watch()
    {
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
        watcher.NotifyFilter = NotifyFilters.LastWrite;
        watcher.Filter = "*.*";
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.EnableRaisingEvents = true;
    }

    private void OnChanged(object source, FileSystemEventArgs e)
    {
        UpdateCollection();
    }

    private void UpdateCollection()
    {
        string path = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);;
        System.IO.DirectoryInfo di = new DirectoryInfo(path);
        CacheFiles = di.GetFiles().ToList();
    }

    public Cleaner()
    {
        UpdateCollection();
        watch();
    }
}