C# 当单击树状视图的根目录时,树状视图已停止工作

C# 当单击树状视图的根目录时,树状视图已停止工作,c#,wpf,xaml,treeview,C#,Wpf,Xaml,Treeview,单击树状视图的根目录时会发生错误 如何解决这个问题 我在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"

单击树状视图的根目录时会发生错误

如何解决这个问题

我在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>
这里是xaml的cs文件

命名空间外部视图 {

}

这里是位置类

命名空间外部视图 {

请帮帮我。 我试着用两天的时间来解决这个问题。
谢谢您的帮助。

您的问题是由显式根TreeView项和绑定到TreeView的SelectedItem的ContentControl引起的

选择根时,绑定会尝试将treeview的根项(即treeview项)作为ContentControl的逻辑子项。因为在WPF中,一个元素只能是另一个元素的逻辑子项,并且因为您的根项已经是treeview的子项,所以会出现异常:

指定的元素已经是另一个元素的逻辑子元素。 先把它断开

如果选择任何其他节点,它都会工作,因为在这些情况下,SelectedItem不是TreeViewItem,而是Location类的实例

要使其正常工作,应使用代码而不是XAML创建根节点:

public class Root
{
    public string name { get; set; }

    public MyLocation Locations { get; set; } 
}
而不是TreeviewRootNode.ItemsSource=locationList;:

XAML:

数据模板:

<HierarchicalDataTemplate x:Key="RootTemplate" ItemsSource="{Binding Locations}"  
  ItemTemplate="{StaticResource LocationTemplate}">
    <StackPanel>
        <Label Content="{Binding name}"/>
    </StackPanel>
 </HierarchicalDataTemplate>

<!--Empty DataTemplate to display the root when selected-->
<DataTemplate DataType="{x:Type local:Root}" />

在2shared.com上的这个项目,我是wpf的新手。在泰国,wpf对图坦卡蒙来说是较少的资源。非常感谢。我很高兴它解决了你的问题。你可以随时与代表1保持平衡。
public class Root
{
    public string name { get; set; }

    public MyLocation Locations { get; set; } 
}
var root = new Root() { Locations = locationList, name = "ทั้งหมด" };
TreeView1.ItemsSource = new List<Root> { root };
<TreeView Grid.Column="0" Grid.Row="0" Name="TreeView1" 
      ItemTemplate="{DynamicResource RootTemplate}" />
<HierarchicalDataTemplate x:Key="RootTemplate" ItemsSource="{Binding Locations}"  
  ItemTemplate="{StaticResource LocationTemplate}">
    <StackPanel>
        <Label Content="{Binding name}"/>
    </StackPanel>
 </HierarchicalDataTemplate>

<!--Empty DataTemplate to display the root when selected-->
<DataTemplate DataType="{x:Type local:Root}" />