How would i display the below XML to a WPF TreeView and it must display as follows
Category Name (Easy Puzzle)
Category Name (Medium Puzzle)
Category Name (Hard Puzzle)
XML
<?xml version="1.0"?>
<SudokuLibrary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Puzzles>
<SudokuPuzzle>
<Category>Easy Puzzle</Category>
<Name>#1</Name>
<Rank>3</Rank>
<Format>
<string>6,0,0,5,0,4,7,0,0</string>
<string>4,9,5,0,0,8,3,6,0</string>
<string>1,0,7,0,0,0,0,0,0</string>
<string>0,5,0,0,0,1,4,2,8</string>
<string>0,0,0,2,0,9,0,0,0</string>
<string>3,2,1,8,0,0,0,5,0</string>
<string>0,0,0,0,0,0,5,0,3</string>
<string>0,1,3,6,0,0,8,7,4</string>
<string>0,0,8,1,0,3,0,0,2</string>
</Format>
<Solution>
<string>6,3,2,5,1,4,7,8,9</string>
<string>4,9,5,7,2,8,3,6,1</string>
<string>1,8,7,9,3,6,2,4,5</string>
<string>7,5,9,3,6,1,4,2,8</string>
<string>8,6,4,2,5,9,1,3,7</string>
<string>3,2,1,8,4,7,9,5,6</string>
<string>9,7,6,4,8,2,5,1,3</string>
<string>2,1,3,6,9,5,8,7,4</string>
<string>5,4,8,1,7,3,6,9,2</string>
</Solution>
<SaveState>
<string>6,0,0,5,0,4,7,0,0</string>
<string>4,9,5,0,0,8,3,6,0</string>
<string>1,0,7,0,0,0,0,0,0</string>
<string>0,5,0,0,0,1,4,2,8</string>
<string>0,0,0,2,0,9,0,0,0</string>
<string>3,2,1,8,0,0,0,5,0</string>
<string>0,0,0,0,0,0,5,0,3</string>
<string>0,1,3,6,0,0,8,7,4</string>
<string>0,0,8,1,0,3,0,0,2</string>
</SaveState>
</SudokuPuzzle>
<SudokuPuzzle>
**...**
</SudokuPuzzle>
</Puzzles>
</SudokuLibrary>
I tried the following below
<ResourceDictionary>
<XmlDataProvider x:Key="xmldata" Source="Data/SudokuLibrary.xml" XPath="/SudokuLibrary/Puzzles/SudokuPuzzle" />
<HierarchicalDataTemplate x:Key="xmldatatemplate" ItemsSource="{Binding}">
<TextBlock Margin="0" Text="{Binding XPath=Name}" />
</HierarchicalDataTemplate>
</ResourceDictionary>
TreeView
<Grid DataContext="{StaticResource xmldata}">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TreeView
x:Name="PuzzleTreeView"
Grid.Row="0"
Width="140"
Padding="3"
Margin="10"
ItemsSource="{Binding}"
SelectedItemChanged="PuzzleTreeView_SelectedItemChanged"/>
Try This..
public class PuzzleName
{
public String Name { get; set; }
public string puzzle{ get; set; }
}
public class Puzzle
{
public String CategoryName { get; set; }
public List<PuzzleName> Children { get; set; }
public Puzzle(string Categoryname)
{
Children = new List<PuzzleName>();
CategoryName= Categoryname;
}
//read the xml file and create an object for the puzzle and load the puzzle name (#1) as its childern
}
In MVVM...
List<Puzzle> puzzleList = new List<Puzzle>
In View...
<TreeView
x:Name="PuzzleTreeView"
Grid.Row="0"
Width="140"
Padding="3"
Margin="10"
ItemsSource="{Binding puzzleList , Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
SelectedItemChanged="PuzzleTreeView_SelectedItemChanged"/>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.