开发者

Treeview in wpf

im trying to bind a ItemsControl to use as an Repeater (Asp.net) inside a static treeview in WPF.

Code.

<Wi开发者_运维知识库ndow x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TreeView Margin="10,10,0,13" Name="TreeView1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="194" Height="200" >
            <TreeViewItem Header="Cold Drinks" IsExpanded="true">
                <TreeViewItem Header="Coke"  IsExpanded="true">
                    <ItemsControl Name="list" >
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <TreeViewItem Header="{Binding}"></TreeViewItem>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </TreeViewItem>
                <TreeViewItem Header="Orange Juice"></TreeViewItem>
            </TreeViewItem>
        </TreeView>
    </StackPanel>
</Window>

C#

list.ItemsSource = new List<string> { "Coke1", "Coke2", "Coke3" };

This results in 3 subitems that seems to be i a group, if i select one i select all.

Anyone?

Thanks, Magnus


Set the ItemsSource-property of the TreeViewItem to your list. This will get you what you expect.
In the following code, I have set the name of your list to the TreeViewItem (as a short hack) for demonstration purposes. So your code will attach the list to the ItemsSource of the TreeViewItem.

<TreeViewItem Header="Cold Drinks" IsExpanded="true">
       <TreeViewItem Header="Coke"  IsExpanded="true" Name="list">                                    
       </TreeViewItem>
       <TreeViewItem Header="Orange Juice">
       </TreeViewItem>
</TreeViewItem>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜