TreeView数据绑定问题WPF C# [英] TreeView Databinding Issue WPF C#
问题描述
我正在使用WPF,C#,Linq到Sql - 我有一个存储过程,我试图将结果绑定到两层深度的树视图。下面是我认为可能有助于更好地理解的一些代码,我不确定是否需要发布这么多代码,但我可以删除不需要的代码。我无法搞清楚数据模板的绑定。任何建议或意见将不胜感激。
这是我正在使用的视图模型的一部分
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;}
}
//this is the parameter I am passing to the procedure
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");
}
}
//This Guid is just for testing the Procedure and binding etc.
//mText = new Guid("579BC2EF-6681-4728-8CC5-9671C1D54A35");
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 - 我想要到顶级显示标题和孩子(第二级是TNumber)
Xaml -- I want to top level to display the Title, and the child(second level to be the TNumber)
<TreeView ItemsSource="{Binding History}">
<!-- template -->
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding History}">
<TextBlock Foreground="Red" Text="{Binding Title}" />
<!-- template -->
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding History}">
<TextBlock Text="{Binding TNumber}" />
<!-- template -->
<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.
// Id TNumber Title Description FullName
//D5C507FB-92FC-4179-BAB9-110928736428 1 Design 1 Random John Doe
//D5C507FB-92FC-4179-BAB9-110928736428 2 Design 1 Another John Two
//E00FE69C-BF14-4261-BEA6-2F4167BCB4F5 1 Design 2 Random Two Jane Doe
//E00FE69C-BF14-4261-BEA6-2F4167BCB4F5 2 Design 2 Another Random Jane Two
//E00FE69C-BF14-4261-BEA6-2F4167BCB4F5 3 Design 2 Third Random Jane Three
//D34CC0DD-F67B-48D6-B0A6-D91F5A4B27E2 1 Design 3 Another Random John Does
//06468A50-CC07-4CEC-B2EC-1817B29DC783 1 Design 4 Full Random John Jane
//06468A50-CC07-4CEC-B2EC-1817B29DC783 2 Design 4 Full text Jane John
尝试显示如下所示。感谢您的任何意见或建议。
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
推荐答案
如果我已正确理解您的问题,您需要创建一个 DataTemplateSelector [ ^ ],看看DBTool for Oracle - 第1部分 [ ^ ]有关如何使用HierarchicalDataTemplate和DataTemplateSelector的相当完整的示例。
你会在Harlinn.Oracle.DBTool \ Types文件夹中找到两个DataTemplateSelector实现。
最好的问候
Espen Harlinn
If I''ve understood your question correctly, you need to create a DataTemplateSelector[^], have a look at DBTool for Oracle - Part 1[^] for a rather complete example on how to work with HierarchicalDataTemplate and DataTemplateSelector.
You''ll find two implementations of DataTemplateSelector in the Harlinn.Oracle.DBTool\Types folder.
Best regards
Espen Harlinn
这篇关于TreeView数据绑定问题WPF C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!