我正在研究 WPF 树控件。我的类有对象,这些对象是对另一个对象的引用。我希望能够在此类中绑定我的对象,以便能够绑定到 xaml 中的树。
这就是我的 ViewModel 中的内容,我想将每个绑定到一个树结构:
class PerformNewVehicleInquiryModel{
public List<AddressDataType> AddressDataVMField { get; set; }
public List<NetworkContolType> NetworkControlVMField { get; set; }
public List<IdentificationType> VehicleIdentificationVMField { get; set; }
public List<PerformNewVehicleInquiryRequestType> PerformNewVehicleInquiryVMRequestField { get; set; }
}
//here is the method to add data to each node
public List<PerformNewVehicleInquiryModel> getAllPerformNewVehicleInquiry()
{
// add to addressDataField List
addressDataField.Add(new AddressDataType("AA", "A1"));
addressDataField.Add(new AddressDataType("AA", "A1"));
// add to networkControlField List
//List<NetworkContolType> networkContolTypeList = new List<NetworkContolType>();
networkControlField.Add(new NetworkContolType(addressDataField, "22", "1305231128040001 1UNISC"));
// create and add to identificationType List
List<IdentificationType> identificationTypeList = new List<IdentificationType>();
identificationTypeList.Add(new IdentificationType("IdentificationType1"));
identificationTypeList.Add(new IdentificationType("IdentificationType1"));
// create and add to PerformNewVehicleInquiryRequestType List
PerformNewVehicleInquiryRequestType performNewVehicleInquiryRequestType = new PerformNewVehicleInquiryRequestType(networkControlField, identificationTypeList);
// add to list
//List<PerformNewVehicleInquiryRequestType> performNewVehicleInquiryRequestTypeList = new List<PerformNewVehicleInquiryRequestType>();
performNewVehicleInquiryRequestField.Add(performNewVehicleInquiryRequestType);
// create and add to PerformNewVehicleInquiryModel
performNewVehicleInquiryModel.Add(new PerformNewVehicleInquiryModel(addressDataField, networkControlField, identificationTypeList, performNewVehicleInquiryRequestField));
return performNewVehicleInquiryModel;
}
// Bind tree to this observable list
public ObservableCollection<PerformNewVehicleInquiryViewModel> PerformNewVehicleInquiries
{
get { return performNewVehicleInquiry; }
set
{
if (performNewVehicleInquiry == value)
return;
performNewVehicleInquiry = value;
NotifyPropertyChanged("PerformNewVehicleInquiries");
}
}
I am having hard time to bind this to a tree structure. it wont display the objects populated in the method above.
// 这是我的 XAML 现在的样子
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding PerformNewVehicleInquiryVMRequestField}">
<TextBlock Foreground="Red" Text="{Binding NetworkContolType }" />
<!-- NetworkControlVMField template -->
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding NetworkControlVMField}">
<TextBlock Text="{Binding AddressData}" />
<!-- AddressDataVMField template -->
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding AddressDataVMField}">
<TextBlock Text="{Binding ApplicationID}" />
<!-- AddressDataVMField template -->
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding MessageOriginatorID }" />
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>