我有两个模型,分别是ExcelFileModel和ExcelSheetModel,如下所示:
using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
public partial class ExcelFileModel : ObservableObject
{
[ObservableProperty] private string _fileName=string.Empty;
[ObservableProperty] private string _filePath=string.Empty;
[ObservableProperty] private bool _isChecked=false;
[ObservableProperty]
private ObservableCollection<ExcelSheetModel> _sheetsCollection = new ObservableCollection<ExcelSheetModel>();
}
public partial class ExcelSheetModel : ObservableObject
{
[ObservableProperty]
private string _sheetName= string.Empty;
[ObservableProperty] private string _parentExcelFileName = string.Empty;
[ObservableProperty] private bool _isChecked=false;
}
字符串
在MainWindowViewModel中:
public partial class MainWindowViewModel : ObservableObject
{
[ObservableProperty] private string _title = "Excel Combine";
[ObservableProperty]
private ObservableCollection<ExcelFileModel> _excelFiles = new ObservableCollection<ExcelFileModel>();
[ObservableProperty]
private ObservableCollection<ExcelSheetModel> _excelSheet = new ObservableCollection<ExcelSheetModel>();
public MainWindowViewModel()
{
ExcelSheet.Add(new ExcelSheetModel() {SheetName = "1", ParentExcelFileName = "FileA"});
ExcelSheet.Add(new ExcelSheetModel() {SheetName = "2", ParentExcelFileName = "FileA"});
ExcelSheet.Add(new ExcelSheetModel() {SheetName = "3", ParentExcelFileName = "FileA"});
ExcelSheet.Add(new ExcelSheetModel() {SheetName = "4", ParentExcelFileName = "FileA"});
ExcelSheet.Add(new ExcelSheetModel() {SheetName = "5", ParentExcelFileName = "FileA"});
ExcelSheet.Add(new ExcelSheetModel() {SheetName = "6", ParentExcelFileName = "FileA"});
ExcelFiles.Add(new ExcelFileModel() {FileName = "FileA", FilePath = "File A Path", SheetsCollection = ExcelSheet});
ExcelFiles.Add(new ExcelFileModel()
{
FileName = "FileB",
FilePath = "File A Path",
SheetsCollection = new ObservableCollection<ExcelSheetModel>()
{
new ExcelSheetModel(){SheetName="Sheet1"},
new ExcelSheetModel(){SheetName="Sheet2"},
}
});
}
}
型
现在我想将ExcelFiles绑定到Syncfusion:SfViewTree,xaml如下:
<syncfusion:SfTreeView
Grid.Column="0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
CheckBoxMode="Recursive"
ItemsSource="{Binding ExcelFiles}"
ChildPropertyName="SheetsCollection"
AutoExpandMode="RootNodes"
Margin="0,0,0,0">
<syncfusion:SfTreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type models:ExcelFileModel}"
ItemsSource="{Binding SheetsCollection}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" IsChecked="{Binding IsChecked}" />
<TextBox Grid.Column="1" Text="{Binding FileName}" />
</Grid>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type models:ExcelSheetModel}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" IsChecked="{Binding IsChecked}" />
<TextBox Grid.Column="1" Text="{Binding SheetName}" />
</Grid>
</HierarchicalDataTemplate>
</syncfusion:SfTreeView.Resources>
型
但是FileName和SheetName没有正确显示,TreeView的相同代码正在工作。我注意到来自Syncfusion的示例使用相同的属性名进行绑定,例如ExcelFileModel中的Name和ExcelSheetModel中的Name。
在这种情况下,我应该如何设置Xaml来显示不同的模型?
我想正确显示Syncfusion:SfTreeView和HierarchicalDataTemplate中的FileName和SheetName。
1条答案
按热度按时间ikfrs5lh1#
目前,Syncfusion SfTreeView不支持HierarchicalDataTemplate作为ItemTemplate。但是,您可以通过创建分层数据模型并将其绑定到SfTreeview来填充数据。有关数据填充的更多信息,请参阅以下用户指南文档,
UG链接:Data Population
分层数据源