ошибка возникает при нажатии на корень дерева.
как исправить эту проблему.
Я загружаю этот проект на 2shared.com
этот проект на2shared.com
скачать здесь
это мой XAML
<Window x:Class="ExTreeview.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ExTreeview"
Title="MainWindow" Height="550" Width="525">
<Window.Resources>
<HierarchicalDataTemplate x:Key="LocationTemplate" ItemsSource="{Binding SubLocation}">
<StackPanel>
<Label Content="{Binding name}"/>
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type local:MyLocation}">
<TextBlock>for root</TextBlock>
</DataTemplate>
<DataTemplate DataType="{x:Type local:house}" > <!-- object from house class use this DataTemplate-->
<StackPanel Margin="0,70,0,0">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60" />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" VerticalAlignment="Center"> ชื่อ</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="0">
ความจุ(ตัว)
</TextBlock>
<TextBlock Grid.Row="2" Grid.Column="0"> สถานะ</TextBlock>
<TextBlock Grid.Row="3" Grid.Column="0"> หมายเหตุ</TextBlock>
<TextBox Grid.Column="1" Grid.Row="0" Height="20" x:Name="txtName" Text="{Binding Path=name}"></TextBox>
<TextBox Grid.Column="1" Grid.Row="1" Width="70" HorizontalAlignment="Left" x:Name="txtCapacity" Text="{Binding Path=capacity}">
</TextBox>
<ComboBox Grid.Column="1" Grid.Row="2" Width="70" HorizontalAlignment="Left" x:Name="cmbStatus" >
<ComboBoxItem Content="{Binding status}" />
</ComboBox>
<TextBox Grid.Column="1" Grid.Row="3" x:Name="txtComment" Text="{Binding Path=comment}"></TextBox>
</Grid>
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Location}" > <!-- object from Location class use this DataTemplate-->
<StackPanel Margin="0,70,0,0" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60" />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" VerticalAlignment="Center"> ชื่อ</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="0"> สถานะ</TextBlock>
<TextBlock Grid.Row="2" Grid.Column="0"> หมายเหตุ</TextBlock>
<TextBox Grid.Column="1" Grid.Row="0" Height="20" x:Name="txtName" Text="{Binding Path=name}"></TextBox>
<ComboBox Grid.Column="1" Grid.Row="1" Width="70" HorizontalAlignment="Left" x:Name="cmbStatus" >
<ComboBoxItem Content="{Binding status}" />
</ComboBox>
<TextBox Grid.Column="1" Grid.Row="2" x:Name="txtComment" Text="{Binding Path=comment}"></TextBox>
</Grid>
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type local:zone}"> <!-- object from zone class use this DataTemplate-->
<StackPanel Margin="0,70,0,0" DataContext="{Binding ElementName=TreeView1, Path=SelectedItem}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60" />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" VerticalAlignment="Center"> ชื่อ</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="0"> สถานะ</TextBlock>
<TextBlock Grid.Row="2" Grid.Column="0"> หมายเหตุ</TextBlock>
<TextBox Grid.Column="1" Grid.Row="0" Height="20" x:Name="txtName" Text="{Binding Path=name}"></TextBox>
<ComboBox Grid.Column="1" Grid.Row="1" Width="70" HorizontalAlignment="Left" x:Name="cmbStatus" >
<ComboBoxItem Content="{Binding status}" />
</ComboBox>
<TextBox Grid.Column="1" Grid.Row="2" x:Name="txtComment" Text="{Binding Path=comment}"></TextBox>
</Grid>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="450" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TreeView Grid.Column="0" Grid.Row="0" Name="TreeView1" >
<TreeViewItem Header="ทั้งหมด" Name="TreeviewRootNode" ItemTemplate="{DynamicResource LocationTemplate}">
</TreeViewItem>
</TreeView>
<StackPanel Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="นำไปใช้งาน" Height="30" Width="80" />
<Button Content="ยกเลิก" Height="30" Margin="15" Width="50" Click="Button_Click" />
</StackPanel>
<ContentControl Content="{Binding ElementName=TreeView1, Path=SelectedItem}" Grid.Column="1" Grid.Row="0">
</ContentControl>
</Grid>
здесь cs файл xaml
пространство имен ExTreeview {
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MyLocation locationList = new MyLocation();
Location location1 = new Location();
location1.name = "ฟาร์มชัยภูมิ";
location1.id = "01";
location1.comment = "คอมเม้น";
location1.status = "ใช้งาน";
location1.SubLocation = new List<zone>();
List<house> sub1 = new List<house>();
sub1.Add(new house() { name = "โรงเรือน ช11", capacity = "50,000", status = "ใช้งาน", comment = "comment" });
sub1.Add(new house() { name = "โรงเรือน ช12", capacity = "30,000", status = "ใช้งาน", comment = "comment" });
location1.SubLocation.Add(new zone() { name = "โซน ช1", SubLocation = sub1, status = "ใช้งาน", comment = "comment" });
List<house> sub2 = new List<house>();
sub2.Add(new house() { name = "โรงเรือน ช21", capacity = "80,000", status = "ใช้งาน", comment = "comment" });
sub2.Add(new house() { name = "โรงเรือน ช22", capacity = "50,000", status = "ใช้งาน", comment = "comment" });
location1.SubLocation.Add(new zone() { name = "โซน ช2", SubLocation = sub2, status = "ใช้งาน", comment = "comment" });
Location location2 = new Location();
location2.name = "ฟาร์มนครนายก";
location2.SubLocation = new List<zone>();
List<house> sub3 = new List<house>();
List<house> sub4 = new List<house>();
List<house> sub6 = new List<house>();
sub3.Add(new house() { name = "โรงเรือนย่อย นย1", capacity = "50,000", status = "ใช้งาน", comment = "comment" });
sub3.Add(new house() { name = "โรงเรือนย่อย นย2", capacity = "60,000", status = "ใช้งาน", comment = "comment" });
sub4.Add(new house() { name = "โซนย่อย นย1", SubLocation = sub3, status = "ใช้งาน", comment = "comment" });
sub4.Add(new house() { name = "โรงเรือน น11", capacity = "50,000", status = "ใช้งาน", comment = "comment" });
sub4.Add(new house() { name = "โรงเรือน น12", capacity = "40,000", status = "ใช้งาน", comment = "comment" });
location2.SubLocation.Add(new zone() { name = "โซน น1", SubLocation=sub4, status = "ใช้งาน", comment = "comment" });
List<house> sub5 = new List<house>();
sub5.Add(new house() { name = "โรงเรือน น21", capacity = "30,000", status = "ใช้งาน", comment = "comment" });
location2.SubLocation.Add(new zone() { name = "โซน น2", SubLocation = sub5, status = "ใช้งาน", comment = "comment" });
locationList.Add(location1);
locationList.Add(location2);
TreeviewRootNode.ItemsSource = locationList;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
здесь Класс местоположения
пространство имен ExTreeview {
public class Location
{
public string name { set; get; }
public string id { set; get; }
public string status { set; get; }
public string comment { set; get; }
public List<zone> SubLocation { set; get; }
public override string ToString()
{
return (this.name);
}
}
public class zone : Location
{
public string capacity { set; get; }
public List<house> SubLocation { set; get; }
public List<house> SubLocationZ { set; get; }
}
public class house : Location
{
public string capacity { set; get; }
public List<house> SubLocation { set; get; }
}
public class MyLocation : List<Location> { }
}
помогите мне, пожалуйста.Я пытаюсь исправить это в течение 2 дней.спасибо за помощь.