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

Dragging gridview and assigning datacontext.

1 Answer 72 Views
DragAndDrop
This is a migrated thread and some comments may be shown as answers.
anurag
Top achievements
Rank 1
anurag asked on 28 Oct 2010, 01:32 AM
i have scnerio like wherin i select a item from my listbox and depending upon the tag of the items i want to show different grids like if i select item1 and drag it then i should be able to see the product detail of that item in a radgridview in destination listbox.Would like to get asap as this is like a showstopper for our coming product.have added the xaml and the code behind file for the same.
<Window x:Class="DragandDrop.Window1"
    xmlns:telerikQuickStart="clr-namespace:Telerik.Windows.Controls.QuickStart;assembly=Telerik.Windows.Controls"
   Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <DataTemplate x:Key="ApplicationDragTemplate">
             <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="50"/>
                    <RowDefinition Height="50"/>
                </Grid.RowDefinitions>
                
                <TextBlock Text="{Binding Name}" Grid.Row="0"  VerticalAlignment="Top" />
                <TextBlock Text="{Binding Author}" Grid.Row="1"  VerticalAlignment="Top" />
  
                <!--<telerik:RadGridView x:Name="Info" Grid.Row="0" RowIndicatorVisibility="Collapsed" IsReadOnly="True"
                              ItemsSource="{Binding Path=ApplicationInfo}" ShowGroupPanel="False"
                                     AutoGenerateColumns="False">
  
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}" Width="110" HeaderTextAlignment="Center" TextAlignment="Left" Header="Name"/>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Author}" Width="80" HeaderTextAlignment="Center"  TextAlignment="Right"  Header="Author"/>
  
  
  
                </telerik:RadGridView.Columns>
  
            </telerik:RadGridView>-->
                  
            </Grid>
              
        </DataTemplate>
        <Style TargetType="ListBoxItem" x:Key="draggableItemStyle">
            <Setter Property="HorizontalAlignment" Value="Stretch" />
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="Foreground" Value="#000000" />
            <Setter Property="telerik:RadDragAndDropManager.AllowDrag" Value="True" />
        </Style>
        </Window.Resources>
      
        <Grid x:Name="LayoutRoot">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="150" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <!--All Applications-->
        <telerikQuickStart:HeaderedContentControl Header="All Applications"
                  
                HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
            <ListBox x:Name="allApplicationsBox" Margin="3" BorderThickness="0"
                    telerik:RadDragAndDropManager.AllowDrop="True" Background="Transparent"
                    ItemContainerStyle="{StaticResource draggableItemStyle}"
                    >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid Width="150">
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            
                            <TextBlock Grid.Row="1" Text="{Binding Name}" FontWeight="Bold"
                                    HorizontalAlignment="Center" />
                            <!--<TextBlock Text="{Binding Author}" Grid.Row="2"
                                    HorizontalAlignment="Center" />-->
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </telerikQuickStart:HeaderedContentControl>
        <!--Text-->
        <Border Grid.Column="1" Background="#1f2759" CornerRadius="5" VerticalAlignment="Center"
                HorizontalAlignment="Center">
          
        </Border>
        <!--My Applications-->
        <telerikQuickStart:HeaderedContentControl Header="My Applications"
                HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
                Grid.Column="2">
            <ListBox x:Name="myApplicationsBox" Margin="3" Background="Transparent"
                    telerik:RadDragAndDropManager.AllowDrop="True" BorderThickness="0"
                    ItemContainerStyle="{StaticResource draggableItemStyle}"
                    >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel  VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="40"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                            <TextBlock Grid.Row="0" Text="{Binding Name}" HorizontalAlignment="Center" />
                             <!--<TextBlock Text="{Binding Author}" HorizontalAlignment="Center" />-->
  
                            <telerik:RadGridView x:Name="View" Grid.Row="1" AllowDrop="True" RowIndicatorVisibility="Collapsed" IsReadOnly="True"
                              ItemsSource="{Binding Path=allApplications}" ShowGroupPanel="False"
                                     AutoGenerateColumns="False">
  
                                <telerik:RadGridView.Columns>
                                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}" Width="110" HeaderTextAlignment="Center" TextAlignment="Left" Header="Name"/>
                                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Author}" Width="80" HeaderTextAlignment="Center"  TextAlignment="Right"  Header="Author"/>
                                </telerik:RadGridView.Columns>
  
                            </telerik:RadGridView
                            </Grid>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <telerik:RadUniformGrid Columns="3" HorizontalAlignment="Left"
                                VerticalAlignment="Top" />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
            </ListBox>
        </telerikQuickStart:HeaderedContentControl>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Telerik.Windows.Controls.DragDrop;
  
namespace DragandDrop
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
  
        public static ObservableCollection<ApplicationInfo> GenerateApplicationInfos()
        {
            ObservableCollection<ApplicationInfo> result = new ObservableCollection<ApplicationInfo>();
  
            ApplicationInfo info1 = new ApplicationInfo();
            info1.Name = "James Collider";
            info1.Author = "Rockford";
            
            result.Add(info1);
  
            ApplicationInfo info2 = new ApplicationInfo();
            info2.Name = "Alentra";
            info2.Author = "Google.";
           
            result.Add(info2);
  
            ApplicationInfo info3 = new ApplicationInfo();
            info3.Name = "Thomas COOK";
            info3.Author = "Yahoo";
            
            result.Add(info3);
  
            ApplicationInfo info4 = new ApplicationInfo();
            info4.Name = "Britsh Airways";
            info4.Author = "Google";
          
  
            ApplicationInfo info5 = new ApplicationInfo();
            info5.Name = "Trader Joe";
            info5.Author = "Factory";
        
  
            ApplicationInfo info6 = new ApplicationInfo();
            info6.Name = "IE Fox";
            info6.Author = "Open Org";
           
            result.Add(info6);
  
            ApplicationInfo info7 = new ApplicationInfo();
            info7.Name = "Charting";
            info7.Author = "PieChart";
         
            result.Add(info7);
  
            ApplicationInfo info8 = new ApplicationInfo();
            info8.Name = "Batman";
            info8.Author = "Games";
        
            result.Add(info8);
  
            return result;
        }
  
        ObservableCollection<ApplicationInfo> allApplications = GenerateApplicationInfos();
        ObservableCollection<ApplicationInfo> myApplications;
        Brush listBoxDragPossible = new SolidColorBrush(Colors.Orange);
  
        public Window1()
        {
              
            InitializeComponent();
            myApplications = new ObservableCollection<ApplicationInfo>();
            allApplicationsBox.ItemsSource = allApplications;
            myApplicationsBox.ItemsSource = myApplications;
              
            RadDragAndDropManager.AddDragQueryHandler(this, OnDragQuery);
            RadDragAndDropManager.AddDragInfoHandler(this, OnDragInfo);
            RadDragAndDropManager.AddDropQueryHandler(this, OnDropQuery);
            RadDragAndDropManager.AddDropInfoHandler(this, OnDropInfo);
  
  
        }
  
        private void OnDropInfo(object sender, DragDropEventArgs e)
        {
            ItemsControl box = e.Options.Destination as ItemsControl;
            if (box == null) return;
            IList<ApplicationInfo> itemsSource = box.ItemsSource as IList<ApplicationInfo>;
            ApplicationInfo payload = e.Options.Payload as ApplicationInfo;
  
            if (e.Options.Status == DragStatus.DropPossible)
            {
                box.BorderBrush = listBoxDragPossible;
            }
            else
            {
                box.BorderBrush = new SolidColorBrush(Colors.Gray);
            }
  
            if (e.Options.Status == DragStatus.DropComplete)
            {
                if (!itemsSource.Contains(payload))
                {
                    itemsSource.Add(payload);
                }
            }
        }
  
        void OnDropQuery(object sender, DragDropQueryEventArgs e)
        {
            ItemsControl box = e.Options.Destination as ItemsControl;
            if (box == null) return;
            IList<ApplicationInfo> itemsSource = box.ItemsSource as IList<ApplicationInfo>;
            ApplicationInfo payload = e.Options.Payload as ApplicationInfo;
  
            e.QueryResult = payload != null && !itemsSource.Contains(payload);
        }
  
        void OnDragInfo(object sender, DragDropEventArgs e)
        {
            try
            {
                ListBoxItem listBoxItem = e.Options.Source as ListBoxItem;
                ListBox box = ItemsControl.ItemsControlFromItemContainer(listBoxItem) as ListBox;
                IList<ApplicationInfo> itemsSource = box.ItemsSource as IList<ApplicationInfo>;
                ApplicationInfo payload = e.Options.Payload as ApplicationInfo;
  
                if (e.Options.Status == DragStatus.DragComplete)
                {
                    if (payload != null && itemsSource.Contains(payload))
                    {
                        itemsSource.Remove(payload);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
  
        protected virtual void OnDragQuery(object sender, DragDropQueryEventArgs e)
        {
            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;
        }
    }
  
    public class ApplicationInfo
    {
       // private Double _price;
        private String _name;
        private String _author;
  
         
  
        
  
        public String Name
        {
            get { return this._name; }
            set { this._name = value; }
        }
  
        public String Author
        {
            get { return this._author; }
            set { this._author = value; }
        }
    }
      
}

1 Answer, 1 is accepted

Sort by
0
Tsvyatko
Telerik team
answered on 01 Nov 2010, 04:37 PM
Hi anurag,

I have prepared sample application demonstrating how to implement the desired functionality. Please have a look at the attachment and let me know if you have any further questions.

All the best,
Tsvyatko
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
DragAndDrop
Asked by
anurag
Top achievements
Rank 1
Answers by
Tsvyatko
Telerik team
Share this question
or