我很难将 aListView
与ObservableCollection
另一个班级的 a 绑定在一起。
我的xml:
<ListView Height="117" HorizontalAlignment="Left" Margin="20,239,0,0" Name="lvResults" VerticalAlignment="Top" Width="759" ItemsSource="{Binding RuleSearch.FileMatches}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding FileName}"/>
<GridViewColumn Header="Size" Width="120" DisplayMemberBinding="{Binding DirectoryName}"/>
<GridViewColumn Header="Date" Width="120" DisplayMemberBinding="{Binding Size}"/>
<GridViewColumn Header="Full Path" Width="120" />
<GridViewColumn Header="Some Meaningless Data" Width="120" />
</GridView>
</ListView.View>
</ListView>
Xaml 背后的代码:
private Search _ruleSearch = new Search();
public Search RuleSearch { get { return _ruleSearch; }}
在搜索类中:
public ObservableCollection<Result> FileMatches { get; private set; }
请注意,更改是在新线程上进行的,如果这有所不同:
private void FindResultOnNewThreads()
{
FileMatches.Clear();
Parallel.ForEach(_fileList, file =>
{
foreach (Regex search in SearchTermList.Where(search => search.IsMatch(file)))
{
lock (FileMatches)
{
FileInfo fileInfo = new FileInfo(file);
FileMatches.Add(new Result
{
Attributes = fileInfo.Attributes,
DirectoryName = fileInfo.DirectoryName,
Extension = fileInfo.Extension,
FileName = fileInfo.Name,
FullNamePath = fileInfo.FullName,
Size = fileInfo.Length
});
}
}
});
}
结果类:
public class Result
{
public string FileName { get; set; }
public string DirectoryName { get; set; }
public string FullNamePath { get; set; }
public long Size { get; set; }
public string Extension { get; set; }
public FileAttributes Attributes { get; set; }
}
真正的问题是,我正在自己学习 wpf,并且无法真正找到 WPF 中数据绑定的规则集。我知道它需要属性和公共属性,除了我被卡住了。