WPF: Widok drzewa przez parsowanie łańcucha
Dlaczego moje drzewo wyświetla tylko 2 poziomy? Na przykład,NetworkControl.AddressData.MessageOriginatorID.Value
pokazuje tylkoNetworkControl
jako root iAddressData
jako dziecko, aleAddressData
nie pokazujeMessageOriginatorID.Value
jako dziecko.
Dane wejściowe:
public List<TreeModel> GetRequestTreeNodes()
{
string[] nodes = {"NetworkControl.AlternateIndexText.Value",
"NetworkControl.AddressData.DestinationID",
"NetworkControl.AddressData.MessageOriginatorID.Value",
"NetworkControl.AddressData.TransactionOriginatorID.Value",
"NetworkControl.AddressData.MessageOriginatorID.Value",
"VehicleIdentification.IdentificationID.Value",
"VehicleSummary.VehicleIdentification.IdentificationID.Value",
"TitleSummary.TitleIdentification.IdentificationID.Value",
"TitleSummary.JurisdictionTitlingKeyText.Value",
"VehicleSummary.VehicleIdentification.IdentificationID.Value",
};
List<TreeModel> nodeList = BuildTree(nodes);
return nodeList;
}
Metoda parsera:
public List<TreeModel> BuildTree(IEnumerable<string> strings)
{
return (
from s in strings
let split = s.Split('.')
group s by s.Split('.')[0] into g
select new TreeModel
{
Name = g.Key,
Children = BuildTree(
from s in g
where s.Length > g.Key.Length + 1
select s.Substring(g.Key.Length + 1))
}
).ToList();
}
Metoda wypełniająca dane w widoku drzewa:
public List<TreeViewModel> GetRequestTreeNodesFromModel()
{
treeNodeViewModel = treeModel.GetRequestTreeNodes().Select(a => new TreeViewModel
{
Children = a.Children.Select(c => new TreeViewModel { Name = c.Name }).ToList(),
Name = a.Name,
}).ToList();
return treeNodeViewModel;
}
XAML
<TreeView Margin="644,137,6,6" Grid.RowSpan="2" ItemsSource="{Binding Path=TreeView}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:TreeViewModel}" ItemsSource="{Binding Path= Children}">
<CheckBox IsChecked="{Binding Name}" Content="{Binding Name}" />
<!--<TextBlock Text="{Binding Name}" />-->
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>