0

I have set the datasource for TreeViewAdv and tried to use the context menu and checkbox for treeview items.

<TreeView>
    <TreeViewItem Header="TestedApps"
         ItemsSource="{Binding Scripts}">
        <StackPanel Orientation="Horizontal">
            <StackPanel.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Play" Click="Play"/>
                </ContextMenu>
            </StackPanel.ContextMenu>
            <CheckBox Margin="0" VerticalAlignment="Center" IsChecked="{Binding IsEnabled, Mode=TwoWay}"/>
            <Rectangle Margin="5 0" Width="12" Height="12" Fill="{Binding Result, Converter={StaticResource ResultConverter}}"/>
            <TextBlock Margin="2 0" Text="{Binding Name}" VerticalAlignment="Center"/>
        </StackPanel>
    </TreeViewItem>
</TreeView>

But it gives me an error "Items collection must be empty before using itemssource". I have visited some articles but could not find the reason for this.

Please anyone let me know how to solve this?

Thanks,

Prithiv
  • 504
  • 5
  • 20
  • Get rid of all the code inside of the treeviewitem. Parser thinks that your `StackPanel` is your items source. What you need is to take that code and insert it into a `DataTemplate` and then reference it in your TreeViewItem. – XAMlMAX Dec 27 '17 at 09:09

1 Answers1

2

I guess, you need the following:

<TreeView>
    <TreeView.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                ....
            </StackPanel>
        </DataTemplate>
    </TreeView.ItemTemplate>    
</TreeView>
JohnyL
  • 6,894
  • 3
  • 22
  • 41