This is a migrated thread and some comments may be shown as answers.

Null Value OnDropQuery ItemSource

0 Answers 47 Views
DragAndDrop
This is a migrated thread and some comments may be shown as answers.
Vinay
Top achievements
Rank 1
Vinay asked on 29 Apr 2011, 01:44 PM
HI,
I have done the code as specified in the Telerik Documentation... But when i try to Drag a Item from ListBox1 to ListBox2 i get an error stating 'Object Referance not set to an Instance of an object' in the OnDropQuery method.

Below is my code, I am getting Data From RIA Service.
In the Below code i am getting Null value in ItemSource specified in DropQuery method...
IList<Customer> itemsSource = box.ItemsSource as IList<Customer>;

<UserControl xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"  x:Class="IATRICTestProject.Views.DragAndDrop"
    xmlns:telerikDragDrop="clr-namespace:Telerik.Windows.Controls.DragDrop;assembly=Telerik.Windows.Controls"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
 
    <UserControl.Resources>
        <DataTemplate x:Key="ApplicationDragTemplate">
            <Image Source="{Binding IconPath}" Stretch="None" VerticalAlignment="Top" />
        </DataTemplate>
        <Style TargetType="ListBoxItem" x:Key="draggableItemStyle">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="telerik:RadDragAndDropManager.AllowDrag" Value="True" />
        </Style>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="150" />
            <ColumnDefinition Width="200" />
        </Grid.ColumnDefinitions>
        <ListBox x:Name="allCustomersBox" telerikDragDrop:RadDragAndDropManager.AutoDrag="True" telerik:RadDragAndDropManager.AllowDrop="True" ItemContainerStyle="{StaticResource draggableItemStyle}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Width="150">
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <TextBlock Text="{Binding Title}" Grid.Row="0" HorizontalAlignment="Center" />                       
                        <TextBlock Grid.Row="1" Text="{Binding FirstName}" FontWeight="Bold" HorizontalAlignment="Center" />
                        <TextBlock Text="{Binding EmailAddress}" Grid.Row="2" HorizontalAlignment="Center" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <!--My Applications-->
        <ListBox x:Name="myCustomersBox" telerikDragDrop:RadDragAndDropManager.AutoDrag="True" telerik:RadDragAndDropManager.AllowDrop="True" ItemContainerStyle="{StaticResource draggableItemStyle}" Grid.Column="2">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                        <TextBlock Text="{Binding EmailAddress}" HorizontalAlignment="Center" />
                        <TextBlock Text="{Binding FirstName}" HorizontalAlignment="Center" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <telerik:RadUniformGrid Columns="3" HorizontalAlignment="Left" VerticalAlignment="Top" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>
    </Grid>
</UserControl>

And here is my Code Behind...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls.DragDrop;
using System.Collections;
using IATRICTestProject.Web;
using System.Collections.ObjectModel;
using System.ServiceModel.DomainServices.Client;
 
namespace IATRICTestProject.Views
{
    public partial class DragAndDrop : UserControl
    {
        private ObservableCollection<Customer> allCustomers;
        private ObservableCollection<Customer> myCustomers;
        AdventureWorksDomainContext _context;       
        public DragAndDrop()
        {
            _context = new AdventureWorksDomainContext();
            InitializeComponent();
            Loaded += new RoutedEventHandler(DragAndDrop_Loaded);                       
 
            RadDragAndDropManager.AddDragQueryHandler(this, OnDragQuery);
            RadDragAndDropManager.AddDragInfoHandler(this, OnDragInfo);
            RadDragAndDropManager.AddDropQueryHandler(this, OnDropQuery);
            RadDragAndDropManager.AddDropInfoHandler(this, OnDropInfo);
        }
 
        void DragAndDrop_Loaded(object sender, RoutedEventArgs e)
        {
            this._context.Load(_context.GetCustomerQuery(), LoadCustomersCompleted, null);
        }
 
        private void LoadCustomersCompleted(LoadOperation<Customer> lo)
        {
            try
            {
                allCustomers = new ObservableCollection<Customer>(_context.Customers);
                allCustomersBox.ItemsSource = allCustomers;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString() + "LoadCustomersCompleted");
            }
        }
 
        #region DragDrop Events
 
        // OnDragQuery event handler
        private void OnDragQuery(object sender, DragDropQueryEventArgs e)
        {
            try
            {
                ListBoxItem listBoxItem = e.Options.Source as ListBoxItem;
                ListBox box = ItemsControl.ItemsControlFromItemContainer(listBoxItem) as ListBox;
                if (e.Options.Status == DragStatus.DragQuery && box != null)
                {
                    e.Options.Payload = box.SelectedItem;
                    ContentControl cue = new ContentControl();
                    cue.ContentTemplate = this.Resources["ApplicationDragTemplate"] as DataTemplate;
                    cue.Content = box.SelectedItem;
                    e.Options.DragCue = cue;
                    e.Options.ArrowCue = RadDragAndDropManager.GenerateArrowCue();
                }
                e.QueryResult = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString() + "OnDragQuery");
            }
        }
 
        // OnDropQuery event handler
        private void OnDropQuery(object sender, DragDropQueryEventArgs e)
        {
            try
            {
                ItemsControl box = e.Options.Destination as ItemsControl;
                IList<Customer> itemsSource = box.ItemsSource as IList<Customer>;
                Customer payload = e.Options.Payload as Customer;
                e.QueryResult = payload != null && !itemsSource.Contains(payload);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString() + "OnDropQuery");
            }
        }
        // OnDropInfo event handler
        private void OnDropInfo(object sender, DragDropEventArgs e)
        {
            try
            {
                ItemsControl box = e.Options.Destination as ItemsControl;
                IList<Customer> itemsSource = box.ItemsSource as IList<Customer>;
                Customer payload = e.Options.Payload as Customer;
                if (e.Options.Status == DragStatus.DropComplete)
                    if (!itemsSource.Contains(payload))
                        itemsSource.Add(payload);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString() + "OnDropInfo");
            }
        }
        // OnDragInfo event handler
        private void OnDragInfo(object sender, DragDropEventArgs e)
        {
            try
            {
                ListBoxItem listBoxItem = e.Options.Source as ListBoxItem;
                ListBox box = ItemsControl.ItemsControlFromItemContainer(listBoxItem) as ListBox;
                IList<Customer> itemsSource = box.ItemsSource as IList<Customer>;
                Customer payload = e.Options.Payload as Customer;
                if (e.Options.Status == DragStatus.DragComplete)
                {
                    if (payload != null && itemsSource.Contains(payload))
                    {
                        itemsSource.Remove(payload);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString() + "OnDragInfo");
            }
        }
 
        #endregion
    }   
 
     
}


No answers yet. Maybe you can help?

Tags
DragAndDrop
Asked by
Vinay
Top achievements
Rank 1
Share this question
or