gpt4 book ai didi

c# - 如何正确绑定(bind) WPF TreeView 控件并有效地显示数据的层次结构?

转载 作者:太空宇宙 更新时间:2023-11-03 22:49:59 58 4
gpt4 key购买 nike

好的,所以我通常会等到最后一刻才提交问题,但我真的需要一些帮助。

我正在尝试绑定(bind) ObservableCollection<Contact_Info>到 WPF TreeView控制。 Contact_Info 包含联系信息的属性并实现 INotifyPropertyChanged .它还包含给定 Contact 的更详细信息列表。 .

我在两件事上遇到了麻烦。 1) 我需要在 TreeViewItem 上显示header 公司名称 但是,很多联系人都缺少公司名称,我想在其中使用名字和姓氏。我曾尝试使用类似 FallBackValue 的东西和 TargetNullValue但 CompanyName 不为空,它只是空的,所以我似乎无法正确理解。我需要转换器吗? 2)我似乎无法获得正确的层次结构,我希望同一 AccountID 的所有联系人在 TreeViewItem 中分组在一起.目前,我只能让公司名称显示在 TreeViewItem 上并展开它显示一个空白值,并且仍然显示重复的公司名称。

我尝试将 HierarchicalDataTemplate 放在 DockPanel.Resources 下的代码(也不确定那是否是正确的位置)。

    <local:Main_Interface x:Key="MyList" />
<HierarchicalDataTemplate DataType = "{x:Type
local:Contact_Info}" ItemsSource = "{Binding Path=OppDataList}">
<TextBlock Text="{Binding Path=CompanyName}"/>
</HierarchicalDataTemplate>
<DataTemplate DataType = "{x:Type sharpspring:SharpspringOpportunityDataModel}" >
<TextBlock Text="{Binding Path=ProjectName}"/>
</DataTemplate>

上面的代码是我在无数其他方法中尝试过的最新方法。我觉得我真的很接近,但在这一点上我真的可以使用 SO 的一些帮助..

其他类(class)有点长,如果您需要更多,请告诉我。非常感谢提供的任何帮助!

编辑 1:Contact_Info

public class Contact_Info : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

internal SharpspringLeadDataModel LeadDataModel;
internal SharpspringOpportunityDataModel OppData;
public List<SharpspringOpportunityDataModel> OppDataList { get; set; }// Should i make this ObservableCollection?

public Project_Info ProjectInfo { get; set; }
public bool IsDirty { get; set; }
public bool IsNew { get; set; }

#region Properties

public long AccountID
{
get => LeadDataModel.AccountID;
set
{
if (value != LeadDataModel.AccountID)
{
LeadDataModel.AccountID = value;
NotifyPropertyChanged();
}
}
}
public long LeadID
{
get => LeadDataModel.LeadID;
set
{
if (value != LeadDataModel.LeadID)
{
LeadDataModel.LeadID = value;
NotifyPropertyChanged();
}
}
}
// Lead Info
public string CompanyName
{
get => LeadDataModel.CompanyName;
set
{
if (value != LeadDataModel.CompanyName)
{
LeadDataModel.FaxNumber = value;
NotifyPropertyChanged();
}
}
}

public Contact_Info(SharpspringLeadDataModel lead, SharpspringOpportunityDataModel opp)
{
LeadDataModel = lead;
OppData = opp;
ProjectInfo = new Project_Info(this);

}

public SharpspringLeadDataModel GetDataModel()
{
return LeadDataModel;
}

public SharpspringOpportunityDataModel GetOppModel()
{
return OppData;
}

public Project_Info GetProjectInfoModel()
{
return ProjectInfo;
}

public void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
IsDirty = true;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

编辑 2:当前填充树但不将公司组合在一起...

private void PopulateTreeView(List<SharpspringLeadDataModel> lead_list, List<SharpspringOpportunityDataModel> opp_list)
{
Contacts = new ObservableCollection<Contact_Info>();
var complete2 =
from lead in lead_list
let oppList = from o in opp_list
where o.PrimaryLeadID == lead.LeadID
select o
select new Contact_Info()
{
LeadDataModel = lead,
OppData = oppList.DefaultIfEmpty(new SharpspringOpportunityDataModel()).First(),
OppDataList = oppList.ToList(),
};

Contacts = complete2.ToObservableCollection();
Lead_Treeview.ItemsSource = Contacts;
}

最佳答案

我认为您的问题实际上是使用 List 而不是 OppDataListObservableCollection。使用您的代码(减少到最少需要)但使用 ObservableCollection 驱动集合,它对我来说很好。

请注意,我的代码可以与 List 一起正常工作,即使因为它都是静态的,但在适当的应用程序中,您需要 OC 来跟踪集合中的更改。

免责声明:我使用隐藏代码来回答这个问题以保持简单,但我不建议在任何真实应用中使用此类代码

主窗口.xaml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="240" Width="320">
<TreeView ItemsSource="{Binding Contacts}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:Contact_Info}" ItemsSource="{Binding Path=OppDataList}">
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate DataType="{x:Type local:SharpspringOpportunityDataModel}">
<TextBlock Text="{Binding Path=ProjectName}" />
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
<TextBlock Text="{Binding Path=CompanyName}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Window>
主窗口.xaml.cs
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using WpfApp1.Annotations;

namespace WpfApp1
{
public partial class MainWindow : INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}

public ObservableCollection<Contact_Info> Contacts { get; } =
new ObservableCollection<Contact_Info>
{
new Contact_Info
{
CompanyName = "Apple",
OppDataList = {new SharpspringOpportunityDataModel {ProjectName = "World take over"}}
},
new Contact_Info
{
CompanyName = "Google",
OppDataList = {new SharpspringOpportunityDataModel {ProjectName = "World take over"}}
},
new Contact_Info
{
CompanyName = "Microsoft",
OppDataList = {new SharpspringOpportunityDataModel {ProjectName = "World take over"}}
}
};


public event PropertyChangedEventHandler PropertyChanged;

[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

public class Contact_Info
{
public string CompanyName { get; set; }

public ObservableCollection<SharpspringOpportunityDataModel> OppDataList { get; } =
new ObservableCollection<SharpspringOpportunityDataModel>();
}

public class SharpspringOpportunityDataModel
{
public string ProjectName { get; set; }
}
}
截屏

Screenshot

关于c# - 如何正确绑定(bind) WPF TreeView 控件并有效地显示数据的层次结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47836241/

58 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com