I am using WPF, C#, Linq to Sql -- I have a stored procedure and I am trying to bind the results to a treeview two levels deep. Below is some of the code I thought might help understand better, I am not sure if I needed to post this much code, but I can remove the uneeded code. I am having trouble figuring out the bindings with the data-templates. Any suggestions or comments would be appreciated.
Here is part of the view model I am using
public sealed class AViewModel : ViewModel
{
public sealed class ItemsToGet
{
public Guid? Id {get; set; }
public int? TNumber { get; set;}
public string Title { get; set; }
public string Description { get; set; }
public string FullName { get; set;}
}
private Guid mText = Guid.Empty;
private ObservableCollection<ItemsToGet> mHistory = new ObservableCollection<ItemsToGet>();
public ObservableCollection<ItemsToGet> History
{
get
{
return mHistory;
}
}
public Guid SearchText
{
get
{
return mText ;
}
set
{
mText = value;
OnPropertyChanged("SearchText");
}
}
var itemlist = from s in context.GetHistory(mText)
select new ItemsToGet()
{
Id = s.Id,
TNumber = s.TNumber,
Title = s.Title,
Description = s.Description,
FullName = s.FullName
};
List<ItemsToGet> Results = itemlist.ToList();
mHistory.Clear();
Results.ForEach(b => mHistory.Add(b));
Here is the View Code Behind
public partial class AView : UserControl
{
public AView()
{
InitializeComponent();
AViewModel vm = this.DataContext as AViewModel;
}
}
Xaml -- I want to top level to display the Title, and the child(second level to be the TNumber)
<TreeView ItemsSource="{Binding History}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding History}">
<TextBlock Foreground="Red" Text="{Binding Title}" />
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding History}">
<TextBlock Text="{Binding TNumber}" />
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</UserControl>
Here is a snapshot of what the results look like, there are more rows than this, but this is the format.
Trying to display it like below. Thanks for any comments or suggestions.
Design 1
--Tnumber 1
--TNumber 2
Design 2
--TNumber 1
--TNumber 2
--TNumber 3
Design 3
--TNumber 1
ETC
ETC