How to load UI elements to a ListBox inside DropTarget control without using DragSourceDropped event?

Jan 18, 2010 at 8:20 PM
Edited Jan 18, 2010 at 8:30 PM

First of all, I like to thank you for this great project. This saved a lot of my time.

I modified "ListBox:add to" and "ListBox: in and between" samples so that there are two list boxes. The ListBox on the left has all the available items(as TextBlock UI Elements) and the one on the left originally empty as in "ListBox:add" example.

User can drag and drop elements from Left ListBox to Right ListBox. Elements in the Right ListBox can be saved to a XML files so that saved elements can be loaded into the Right ListBox from the elements saved in XML file when user opens this page again.

In the MainPage_Loaded method,  I use rightListBox.ItemsSource to bind the UI elements that was saved in the XML file and I get Null reference on right ListBox (rightListBox). However, I can successfully bind to a List Box which is not under <dd:DropTarget/> or <dd:DragSource/>  controls. Is there any way I can bind UI elements to rightListBox in the page load without using DragSourceDropped="DropTarget_DragSourceDropped1" event?  Please find my code below. I really appreciate your help. 

        <StackPanel Orientation="Vertical" HorizontalAlignment="Center"

                           Grid.Row="2" Grid.Column="1" Margin="10">

            <TextBlock FontSize="14" Margin="0,0,0,10">The second list</TextBlock>

            <dd:DropTarget x:Name="dropTarget" HorizontalAlignment="Center"

                                 DragSourceDropped="DropTarget_DragSourceDropped1" 

                                 VerticalAlignment="Top" >

                <dd:DropTarget.Content>

                    <dd:DragSource DraggingEnabled="False" AllDropTargetsValid="False" DragHandleMode="FullDragSource" DropMode="DropDragSource">

                        <ListBox x:Name="rightListBox" Loaded="rightListBox_Loaded" Width="200" Height="400">

                            <ListBox.ItemTemplate>

                                <DataTemplate>

                                    <dd:DropTarget Width="200" Height="40"

                                       DragSourceDropped="DropTarget_DragSourceDropped1" >

                                        <dd:DropTarget.Content>

                                            <dd:DragSource ShowSwitchReplaceAnimation="False" AllDropTargetsValid="True" DropMode="DropDragSource">

                                                <Grid Width="200" Height="40" Background="{Binding Background}">

                                                    <TextBlock Text="{Binding Dummy}" Loaded="TextBlock_Loaded"

                                                   FontSize="14" VerticalAlignment="Center"

                                                     HorizontalAlignment="Center"></TextBlock>

                                                </Grid>

                                            </dd:DragSource>

                                        </dd:DropTarget.Content>

                                    </dd:DropTarget>

                                </DataTemplate>

                            </ListBox.ItemTemplate>

                        </ListBox>

                    </dd:DragSource>

                </dd:DropTarget.Content>

            </dd:DropTarget>

        </StackPanel>

 

        void MainPage_Loaded(object sender, RoutedEventArgs e)

        {

            loadItems = new ObservableCollection<ListContent>();

             foreach (XElement node in elements)

            {

                loadItems.Add(new ListContent() { Dummy = node.Value, Background = getGradient(false) });

            }

            rightListBox.ItemsSource = loadItems;              //Error Here

        }

}

Coordinator
Jan 27, 2010 at 7:09 PM

Have you tried setting the ItemsSource after the droptarget/listbox has been loaded (so after its Loaded event has been triggered)?

 

Kind regards,

Kevin. 

Jan 28, 2010 at 2:13 AM

Thanks Kevin.. This should do the trick. Thanks for your great help.