I am having a hard time binding a ListView with an ObservableCollection in another class.
My xaml:
<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>
The Xaml behind code:
private Search _ruleSearch = new Search();
public Search RuleSearch { get { return _ruleSearch; }}
In Search class:
public ObservableCollection<Result> FileMatches { get; private set; }
Note the changes are made on a new thread, if that makes a difference:
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
});
}
}
});
}
Result class:
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; }
}
Issue really is, I am learning wpf by myself and couldn’t really find a rule set for data binding in WPF. I know that it requires properties and public ones, other than that I am stuck.
Probably your listViews DataContext is not set to the UserControls or Windows or whatever, where you have your RuleSearch prop.
You can set it in the xaml.cs codebehind.
or in xaml
where typeOfAncestor is the type of your usercontrol/window…