Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 10.44 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. TreeView has stop working when click on the root of the treeview
  2. <Window x:Class="ExTreeview.MainWindow"
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5.     xmlns:local="clr-namespace:ExTreeview"
  6.     Title="MainWindow" Height="550" Width="525">
  7. <Window.Resources>
  8.     <HierarchicalDataTemplate x:Key="LocationTemplate" ItemsSource="{Binding SubLocation}">
  9.         <StackPanel>
  10.             <Label Content="{Binding name}"/>
  11.         </StackPanel>
  12.     </HierarchicalDataTemplate>
  13.     <DataTemplate DataType="{x:Type local:MyLocation}">
  14.         <TextBlock>for root</TextBlock>
  15.     </DataTemplate>
  16.     <DataTemplate DataType="{x:Type local:house}" > <!-- object from house class use this DataTemplate-->
  17.         <StackPanel Margin="0,70,0,0">
  18.  
  19.             <Grid>
  20.                 <Grid.RowDefinitions>
  21.                     <RowDefinition Height="60" />
  22.                     <RowDefinition />
  23.                     <RowDefinition />
  24.                     <RowDefinition />
  25.                 </Grid.RowDefinitions>
  26.                 <Grid.ColumnDefinitions>
  27.                     <ColumnDefinition Width="70" />
  28.                     <ColumnDefinition />
  29.                 </Grid.ColumnDefinitions>
  30.  
  31.                 <TextBlock Grid.Row="0" Grid.Column="0" VerticalAlignment="Center"> ชื่อ</TextBlock>
  32.                 <TextBlock Grid.Row="1" Grid.Column="0">
  33.                 ความจุ(ตัว)
  34.                 </TextBlock>
  35.                 <TextBlock Grid.Row="2" Grid.Column="0"> สถานะ</TextBlock>
  36.                 <TextBlock Grid.Row="3" Grid.Column="0"> หมายเหตุ</TextBlock>
  37.  
  38.                 <TextBox Grid.Column="1" Grid.Row="0" Height="20" x:Name="txtName" Text="{Binding Path=name}"></TextBox>
  39.                 <TextBox Grid.Column="1" Grid.Row="1" Width="70" HorizontalAlignment="Left" x:Name="txtCapacity" Text="{Binding Path=capacity}">
  40.  
  41.                 </TextBox>
  42.                 <ComboBox Grid.Column="1" Grid.Row="2" Width="70" HorizontalAlignment="Left" x:Name="cmbStatus"  >
  43.                     <ComboBoxItem Content="{Binding status}" />
  44.                 </ComboBox>
  45.                 <TextBox Grid.Column="1" Grid.Row="3" x:Name="txtComment" Text="{Binding Path=comment}"></TextBox>
  46.  
  47.             </Grid>
  48.         </StackPanel>
  49.     </DataTemplate>
  50.     <DataTemplate DataType="{x:Type local:Location}" > <!-- object from Location class use this DataTemplate-->
  51.         <StackPanel Margin="0,70,0,0" >
  52.  
  53.             <Grid>
  54.                 <Grid.RowDefinitions>
  55.                     <RowDefinition Height="60" />
  56.                     <RowDefinition />
  57.                     <RowDefinition />
  58.                 </Grid.RowDefinitions>
  59.                 <Grid.ColumnDefinitions>
  60.                     <ColumnDefinition Width="70" />
  61.                     <ColumnDefinition />
  62.                 </Grid.ColumnDefinitions>
  63.  
  64.                 <TextBlock Grid.Row="0" Grid.Column="0" VerticalAlignment="Center"> ชื่อ</TextBlock>
  65.  
  66.                 <TextBlock Grid.Row="1" Grid.Column="0"> สถานะ</TextBlock>
  67.                 <TextBlock Grid.Row="2" Grid.Column="0"> หมายเหตุ</TextBlock>
  68.  
  69.                 <TextBox Grid.Column="1" Grid.Row="0" Height="20" x:Name="txtName" Text="{Binding Path=name}"></TextBox>
  70.  
  71.  
  72.                 <ComboBox Grid.Column="1" Grid.Row="1" Width="70" HorizontalAlignment="Left" x:Name="cmbStatus"  >
  73.                     <ComboBoxItem Content="{Binding status}" />
  74.                 </ComboBox>
  75.                 <TextBox Grid.Column="1" Grid.Row="2" x:Name="txtComment" Text="{Binding Path=comment}"></TextBox>
  76.  
  77.             </Grid>
  78.         </StackPanel>
  79.     </DataTemplate>
  80.     <DataTemplate DataType="{x:Type local:zone}"> <!-- object from zone class use this DataTemplate-->
  81.         <StackPanel Margin="0,70,0,0" DataContext="{Binding ElementName=TreeView1, Path=SelectedItem}">
  82.  
  83.             <Grid>
  84.                 <Grid.RowDefinitions>
  85.                     <RowDefinition Height="60" />
  86.                     <RowDefinition />
  87.                     <RowDefinition />
  88.                 </Grid.RowDefinitions>
  89.                 <Grid.ColumnDefinitions>
  90.                     <ColumnDefinition Width="70" />
  91.                     <ColumnDefinition />
  92.                 </Grid.ColumnDefinitions>
  93.  
  94.                 <TextBlock Grid.Row="0" Grid.Column="0" VerticalAlignment="Center"> ชื่อ</TextBlock>
  95.  
  96.                 <TextBlock Grid.Row="1" Grid.Column="0"> สถานะ</TextBlock>
  97.                 <TextBlock Grid.Row="2" Grid.Column="0"> หมายเหตุ</TextBlock>
  98.  
  99.                 <TextBox Grid.Column="1" Grid.Row="0" Height="20" x:Name="txtName" Text="{Binding Path=name}"></TextBox>
  100.  
  101.  
  102.                 <ComboBox Grid.Column="1" Grid.Row="1" Width="70" HorizontalAlignment="Left" x:Name="cmbStatus"  >
  103.                     <ComboBoxItem Content="{Binding status}" />
  104.                 </ComboBox>
  105.                 <TextBox Grid.Column="1" Grid.Row="2" x:Name="txtComment" Text="{Binding Path=comment}"></TextBox>
  106.  
  107.             </Grid>
  108.         </StackPanel>
  109.     </DataTemplate>
  110.  
  111. </Window.Resources>
  112. <Grid>
  113.     <Grid.RowDefinitions>
  114.         <RowDefinition Height="450" />
  115.         <RowDefinition />
  116.     </Grid.RowDefinitions>
  117.     <Grid.ColumnDefinitions>
  118.         <ColumnDefinition />
  119.         <ColumnDefinition />
  120.     </Grid.ColumnDefinitions>
  121.  
  122.     <TreeView Grid.Column="0" Grid.Row="0" Name="TreeView1" >
  123.         <TreeViewItem Header="ทั้งหมด" Name="TreeviewRootNode" ItemTemplate="{DynamicResource LocationTemplate}">
  124.         </TreeViewItem>
  125.  
  126.     </TreeView>
  127.  
  128.     <StackPanel Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Right">
  129.         <Button Content="นำไปใช้งาน" Height="30" Width="80" />
  130.         <Button Content="ยกเลิก" Height="30" Margin="15" Width="50" Click="Button_Click" />
  131.     </StackPanel>
  132.  
  133.  
  134.     <ContentControl Content="{Binding ElementName=TreeView1, Path=SelectedItem}"  Grid.Column="1" Grid.Row="0">
  135.  
  136.     </ContentControl>
  137.  
  138. </Grid>
  139.        
  140. public partial class MainWindow : Window
  141. {
  142.     public MainWindow()
  143.     {
  144.         InitializeComponent();
  145.  
  146.         MyLocation locationList = new MyLocation();
  147.  
  148.         Location location1 = new Location();
  149.         location1.name = "ฟาร์มชัยภูมิ";
  150.         location1.id = "01";
  151.         location1.comment = "คอมเม้น";
  152.         location1.status = "ใช้งาน";
  153.         location1.SubLocation = new List<zone>();
  154.  
  155.         List<house> sub1 = new List<house>();
  156.         sub1.Add(new house() { name = "โรงเรือน ช11", capacity = "50,000", status = "ใช้งาน", comment = "comment" });
  157.         sub1.Add(new house() { name = "โรงเรือน ช12", capacity = "30,000", status = "ใช้งาน", comment = "comment" });
  158.         location1.SubLocation.Add(new zone() { name = "โซน ช1", SubLocation = sub1, status = "ใช้งาน", comment = "comment" });
  159.  
  160.         List<house> sub2 = new List<house>();
  161.         sub2.Add(new house() { name = "โรงเรือน ช21", capacity = "80,000", status = "ใช้งาน", comment = "comment" });
  162.         sub2.Add(new house() { name = "โรงเรือน ช22", capacity = "50,000", status = "ใช้งาน", comment = "comment" });
  163.         location1.SubLocation.Add(new zone() { name = "โซน ช2", SubLocation = sub2, status = "ใช้งาน", comment = "comment" });
  164.  
  165.         Location location2 = new Location();
  166.         location2.name = "ฟาร์มนครนายก";
  167.         location2.SubLocation = new List<zone>();
  168.  
  169.         List<house> sub3 = new List<house>();
  170.         List<house> sub4 = new List<house>();
  171.         List<house> sub6 = new List<house>();
  172.         sub3.Add(new house() { name = "โรงเรือนย่อย นย1", capacity = "50,000", status = "ใช้งาน", comment = "comment" });
  173.         sub3.Add(new house() { name = "โรงเรือนย่อย นย2", capacity = "60,000", status = "ใช้งาน", comment = "comment" });
  174.         sub4.Add(new house() { name = "โซนย่อย นย1", SubLocation = sub3, status = "ใช้งาน", comment = "comment" });
  175.  
  176.  
  177.         sub4.Add(new house() { name = "โรงเรือน น11", capacity = "50,000", status = "ใช้งาน", comment = "comment" });
  178.         sub4.Add(new house() { name = "โรงเรือน น12", capacity = "40,000", status = "ใช้งาน", comment = "comment" });
  179.  
  180.  
  181.         location2.SubLocation.Add(new zone() { name = "โซน น1", SubLocation=sub4, status = "ใช้งาน", comment = "comment" });
  182.  
  183.         List<house> sub5 = new List<house>();
  184.         sub5.Add(new house() { name = "โรงเรือน น21", capacity = "30,000", status = "ใช้งาน", comment = "comment" });
  185.         location2.SubLocation.Add(new zone() { name = "โซน น2", SubLocation = sub5, status = "ใช้งาน", comment = "comment" });
  186.  
  187.         locationList.Add(location1);
  188.         locationList.Add(location2);
  189.  
  190.         TreeviewRootNode.ItemsSource = locationList;
  191.  
  192.     }
  193.  
  194.     private void Button_Click(object sender, RoutedEventArgs e)
  195.     {
  196.         this.Close();
  197.     }
  198.  
  199. }
  200.        
  201. public class Location
  202. {
  203.     public string name { set; get; }
  204.     public string id { set; get; }
  205.     public string status { set; get; }
  206.     public string comment { set; get; }
  207.  
  208.     public List<zone> SubLocation { set; get; }
  209.  
  210.     public override string ToString()
  211.     {
  212.         return (this.name);
  213.     }
  214.  
  215. }
  216. public class zone : Location
  217. {
  218.     public string capacity { set; get; }
  219.     public List<house> SubLocation { set; get; }
  220.     public List<house> SubLocationZ { set; get; }
  221. }
  222.  
  223. public class house : Location
  224. {
  225.     public string capacity { set; get; }
  226.     public List<house> SubLocation { set; get; }
  227. }
  228.  
  229. public class MyLocation : List<Location> { }
  230.  
  231.  
  232. }
  233.        
  234. public class Root
  235. {
  236.     public string name { get; set; }
  237.  
  238.     public MyLocation Locations { get; set; }
  239. }
  240.        
  241. var root = new Root() { Locations = locationList, name = "ทั้งหมด" };
  242. TreeView1.ItemsSource = new List<Root> { root };
  243.        
  244. <TreeView Grid.Column="0" Grid.Row="0" Name="TreeView1"
  245.       ItemTemplate="{DynamicResource RootTemplate}" />
  246.        
  247. <HierarchicalDataTemplate x:Key="RootTemplate" ItemsSource="{Binding Locations}"  
  248.   ItemTemplate="{StaticResource LocationTemplate}">
  249.     <StackPanel>
  250.         <Label Content="{Binding name}"/>
  251.     </StackPanel>
  252.  </HierarchicalDataTemplate>
  253.  
  254. <!--Empty DataTemplate to display the root when selected-->
  255. <DataTemplate DataType="{x:Type local:Root}" />