开发者

Treeview binding problem

开发者 https://www.devze.com 2023-02-27 06:02 出处:网络
I have a window MainWindow.xaml and private static Tutorial tutorial; there. Also I have class Structure.cs where I describe child types

I have a window MainWindow.xaml and

private static Tutorial tutorial; there.

Also I have class Structure.cs where I describe child types

public class Tutorial
{
    public string Name { get; set; }
    public IList<Chapter> Chapters = new List<Chapter>();
}


public class Chapter
{
    public string Name { get; set; }
    public IList<Unit> Units = new List<Unit>();
}

public class Unit
{
    public string Name { get; set; }
    public IList<Frame> Frames = new List<Frame>();
    ... 

}

I want to bind tutorial structure to treeview. How can I do this?

I tried this way.

                    <TreeView Grid.Row="2" x:Name="treeViewStruct" Margin="5,0,5,0" Background="LemonChiffon" BorderBrush="Bisque" BorderThickness="1" ScrollViewer.VerticalScrollBarVisibility="Auto" IsTextSearchEnabled="True" Cursor="Hand">
                        <TreeView.Resources>
                            <HierarchicalDataTemplate DataType    = "{x:Type Structure:Chapter}"
                            ItemsSource = "{Bindi开发者_JAVA技巧ng Path=Units}">
                                <TextBlock Text="{Binding Path=Name}"/>
                            </HierarchicalDataTemplate>

                            <DataTemplate DataType="{x:Type Structure:Unit}">
                                <TextBlock Text="{Binding Path=Name}"/>
                        </DataTemplate>
                        </TreeView.Resources>
                    </TreeView>

It doesn't work. Please, help! I'm a newbie in WPF. I need dynamic tree so that when I add a chapter or a unit in the object tutorial, tree is updated. And for this way of binding please throw the idea how can I get a collection item, when I selected some tree node.


This may help :

<HierarchicalDateTemplate DataType = "{x:Type local:Tutorial}"
           ItemsSource="{Binding Chapters}">
           <TextBlock Text="{Binding Name}"/>
    </HierarchicalDateTemplate>

    <HierarchicalDateTemplate DataType = "{x:Type local:Chapter}"
           ItemsSource="{Binding Units}"
           <TextBlock Text="{Binding Name}"/>
    </HierarchicalDateTemplate>

    <DateTemplate DataType = "{x:Type local:Unit}"
           <TextBlock Text="{Binding Name}"/>
    </DateTemplate>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号