简体   繁体   English

WPF Treeview问题

[英]Wpf Treeview issue

How would i display the below XML to a WPF TreeView and it must display as follows 我将如何显示以下XML到WPF TreeView,它必须显示如下

Category Name (Easy Puzzle) 类别名称(简易拼图)

  • Puzzle Name (#1) 拼图名称(#1)

Category Name (Medium Puzzle) 类别名称(中号拼图)

  • Puzzle Name (#1) 拼图名称(#1)

Category Name (Hard Puzzle) 类别名称(硬拼图)

  • Puzzle Name (#1) 拼图名称(#1)

XML 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... 在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"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM