一、概述
关于DataGrid指定Row的颜色,我们可以使用转换器和DataGridRow的Style来实现。对于文件的检测,我们可以使用FileSystemWatcher来实现。
二、Demo
Converter代码如下:
1 using System;
2 using System.Windows.Data;
3 using System.Windows.Media;
4
5 namespace FileWatcher
6 {
7 public class BGConverter : IValueConverter
8 {
9 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
10 {
11 string operation = (string)value;
12 switch(operation)
13 {
14 case "Created":
15 return Brushes.Green;
16
17 case "Deleted":
18 return Brushes.Red;
19
20 case "Renamed":
21 return Brushes.Orange;
22 default:
23 return Brushes.Black;
24 }
25
26 }
27
28 public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
29 {
30 return null;
31 }
32 }
33 }
前台代码如下:
1
后台代码如下:
1 using System;
2 using System.Collections.Generic;
3 using System.Collections.ObjectModel;
4 using System.ComponentModel;
5 using System.IO;
6 using System.Linq;
7 using System.Windows;
8
9 namespace FileWatcher
10 {
11 public abstract class ViewModelBase : INotifyPropertyChanged
12 {
13 public virtual string DisplayName { get; set; }
14
15 public event PropertyChangedEventHandler PropertyChanged;
16
17 protected void OnPropertyChanged(string propertyName)
18 {
19 PropertyChangedEventHandler handler = PropertyChanged;
20
21 if (handler != null)
22 {
23 handler(this, new PropertyChangedEventArgs(propertyName));
24 }
25 }
26
27 }
28 ///
31 public partial class MainWindow : Window, INotifyPropertyChanged
32 {
33 public event PropertyChangedEventHandler PropertyChanged;
34
35 protected void OnPropertyChanged(string propertyName)
36 {
37 PropertyChangedEventHandler handler = PropertyChanged;
38
39 if (handler != null)
40 {
41 handler(this, new PropertyChangedEventArgs(propertyName));
42 }
43 }
44 private List
45 private ObservableCollection
46 public ObservableCollection
47 {
48 get { return fileCollection; }
49 set
50 {
51 fileCollection = value;
52 OnPropertyChanged("FileCollection");
53 }
54 }
55 private bool isAllChecked;
56 public bool IsAllChecked
57 {
58 get { return isAllChecked; }
59 set
60 {
61 isAllChecked = value;
62 OnPropertyChanged("IsAllChecked");
63
64 for (int i = 0; i < FileCollection.Count; i++)
65 {
66 FileCollection[i].IsChecked = isAllChecked;
67 }
68 //OnPropertyChanged("FilaCalibrationProtocols");
69 }
70 }
71 FileSystemWatcher fsw = new FileSystemWatcher();
72 public MainWindow()
73 {
74 InitializeComponent();
75 dataGrid.DataContext = this;
76 fsw.Path = @"C:\"; //设置监控的文件目录
77 fsw.Filter = "*.*"; //设置监控文件的类型
78 fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size; //设置文件的文件名、目录名及文件的大小改动会触发Changed事件
79
80 fsw.Created += new FileSystemEventHandler(this.fileSystemWatcher_EventHandle); //绑定事件触发后处理数据的方法。
81
82 fsw.Deleted += new FileSystemEventHandler(this.fileSystemWatcher_EventHandle);
83
84 fsw.Changed += new FileSystemEventHandler(this.fileSystemWatcher_EventHandle);
85
86 fsw.Renamed += new RenamedEventHandler(this.fileSystemWatcher_Renamed); //重命名事件与增删改传递的参数不一样。
87
88 }
89 private void fileSystemWatcher_EventHandle(object sender, FileSystemEventArgs e) //文件增删改时被调用的处理方法
90 {
91 this.Dispatcher.BeginInvoke(
92 new Action(() =>
93 {
94
95 var fileInfo = new FileInfo() { Operation = e.ChangeType.ToString(), Date = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"), FileName = e.Name, FilePath = e.FullPath, IsChecked = false };
96
97 if(e.ChangeType.ToString() == "Created")
98 {
99 var fileName = from file in fileList.Distinct()
100 where file == e.Name
101 select file;
102 if (fileName.Count() > 0)
103 {
104 fileInfo.Note = $"{e.Name} have already been {e.ChangeType} before.";
105 MessageBox.Show($"{e.Name} have already been {e.ChangeType} before.");
106 }
107 }
108
109 fileCollection.Add(fileInfo);
110
111 fileList.Add(e.Name);
112 })
113 );
114
115 }
116 private void fileSystemWatcher_Renamed(object sender, RenamedEventArgs e) //文件重命名时被调用的处理方法
117 {
118 this.Dispatcher.BeginInvoke(
119 new Action(() =>
120 {
121 fileCollection.Add(new FileInfo() { Operation = e.ChangeType.ToString(), Date = DateTime.Now.ToLongTimeString(), FileName = e.OldName + " --> " + e.Name, FilePath = e.FullPath, IsChecked = false });
122 fileList.Add(e.Name);
123 })
124 );
125
126 }
127
128 private void Button_Click(object sender, RoutedEventArgs e)
129 {
130 if(Directory.Exists(tbxPath.Text))
131 {
132 fsw.Path = tbxPath.Text;
133 }
134 fsw.EnableRaisingEvents = true;
135 }
136 }
137 public class FileInfo : ViewModelBase
138 {
139 private bool isChecked;
140
141 public bool IsChecked
142 { get { return isChecked; } set { isChecked = value; OnPropertyChanged("IsChecked"); } }
143
144
145 private string fileName;
146 public string FileName
147 {
148 get { return fileName; }
149 set { fileName = value; }
150 }
151
152 private string filePath;
153 public string FilePath
154 {
155 get { return filePath; }
156 set { filePath = value; }
157 }
158
159 private string operation;
160 public string Operation
161 {
162 get { return operation; }
163 set { operation = value; }
164 }
165
166 private string date;
167 public string Date
168 {
169 get { return date; }
170 set { date = value; }
171 }
172
173 private string note;
174 public string Note
175 {
176 get { return note; }
177 set { note = value; }
178 }
179
180 }
181 }
运行结果如下:
手机扫一扫
移动阅读更方便
你可能感兴趣的文章