Drag Stared on TreeView Selection_Canged Event

2 posts, 0 answers
  1. Gaurang
    Gaurang avatar
    29 posts
    Member since:
    Dec 2010

    Posted 12 Sep 2011 Link to this post

    Hi,

    I am creating a control for User document management. In that there is a User Folder Tree(Left Side) and Related document on (Right Side). When user click on particular folder I try show list of doucments on right hand side Gridview as well as I have implement drag and drop facility to move one folder to another and document also using drag and drop functionality.
    But my problem is when SelectionChanged event call automaticaly DragStarted and start droping funcatinality.

    But I want When user just click on folder that time only SelectinChanged event call and open the list of documents. If user Click on Folder and drag the fodler that time DragStated event should call. But it is not happening.

    I have past here sample code (I try to convet into zip and attached but did not attached so). Please help me.

    1)Page.xaml
    --------------
    <UserControl x:Class="TreeViewDragDropBug.Page"
    xmlns:nav="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"
    xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"
    xmlns:my="clr-namespace:TreeViewDragDropBug"
    Width="400" Height="300">
    <UserControl.Resources>
    <telerik:HierarchicalDataTemplate x:Name="myTemplate" ItemsSource="{Binding Children}">
    <TextBlock Text="{Binding Name}"/>
    </telerik:HierarchicalDataTemplate>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
    <nav:RadTreeView x:Name="myTreeView" ItemTemplate="{StaticResource myTemplate}" IsDragDropEnabled="True"/>
    </Grid>
    </UserControl>
    ---------------------------------------------------------------------------------------------------------------------------------
    2)Page.xaml.cs
    -----------------
    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 System.Collections;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Diagnostics;
    using Telerik.Windows.Input;
    namespace TreeViewDragDropBug
    {
    public partial class Page : UserControl
    {
    IList<TestItemsSouce> iSource;
    TestItemsSouce item5;
    TestItemsSouce item7;
    TestItemsSouce item1;
    TestItemsSouce item2;
    TestItemsSouce item3;
    public Page()
    {
    InitializeComponent();
    myTreeView.SelectionChanged += new Telerik.Windows.Controls.SelectionChangedEventHandler(myTreeView_SelectionChanged);
    myTreeView.DragStarted += new Telerik.Windows.Controls.RadTreeViewDragEventHandler(myTreeView_DragStarted);
    myTreeView.DragEnded += new Telerik.Windows.Controls.RadTreeViewDragEndedEventHandler(myTreeView_DragEnded);
    #region Data
    iSource = new List<TestItemsSouce>()
    {
    new TestItemsSouce()
    {
    Name = "root",
    Children = new ObservableCollection<TestItemsSouce>()
    {
    new TestItemsSouce()
    {
    Name = "item 1"
    },
    new TestItemsSouce()
    {
    Name = "item 2",
    Children = new ObservableCollection<TestItemsSouce>()
    {
    new TestItemsSouce()
    {
    Name = "item 5",
    Children = new ObservableCollection<TestItemsSouce>()
    {
    new TestItemsSouce()
    {
    Name = "item 6"
    }
    }
    }
    }
    },
    new TestItemsSouce()
    {
    Name = "item 3",
    Children = new ObservableCollection<TestItemsSouce>()
    {
    new TestItemsSouce()
    {
    Name = "item 7",
    }
    }
    },
    new TestItemsSouce()
    {
    Name = "item 4"
    }
    }
    }
    };
    #endregion
    item2 = this.iSource[0].Children[1];
    item1 = this.iSource[0].Children[0];
    item7 = this.iSource[0].Children[2].Children[0];
    item3 = this.iSource[0].Children[2];
    item5 = this.iSource[0].Children[1].Children[0];
    myTreeView.ItemsSource = iSource;
    }
    void myTreeView_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
    {
    //throw new NotImplementedException();
    var selectedItem = e.AddedItems[0] as TestItemsSouce;
    if (selectedItem != null)
    MessageBox.Show("Selection Change." + selectedItem.ToString());
    else
    MessageBox.Show("Selection Change");
    }
    void myTreeView_DragEnded(object sender, Telerik.Windows.Controls.RadTreeViewDragEndedEventArgs e)
    {
    //throw new NotImplementedException();
    MessageBox.Show("Drag Ended");
    }
    void myTreeView_DragStarted(object sender, Telerik.Windows.Controls.RadTreeViewDragEventArgs e)
    {
    //throw new NotImplementedException();
    MessageBox.Show("Drag Started");
    }
    public class TestItemsSouce : INotifyPropertyChanged
    {
    public TestItemsSouce()
    {
    Children = new ObservableCollection<TestItemsSouce>();
    }
    private ObservableCollection<TestItemsSouce> children;
    /// <summary>
    /// Gets or sets true.
    /// </summary>
    public ObservableCollection<TestItemsSouce> Children
    {
    get
    {
    return this.children;
    }
    set
    {
    if (this.children != value)
    {
    this.children = value;
    OnPropertyChanged("Children");
    }
    }
    }
    private String name;
    public String Name
    {
    get
    {
    return this.name;
    }
    set
    {
    if (this.name != value)
    {
    this.name = value;
    OnPropertyChanged("Name");
    }
    }
    }
    public override string ToString()
    {
    return String.Format("OIS: {0}", name);
    }
    protected virtual void OnPropertyChanged(String propertyName)
    {
    if (String.IsNullOrEmpty(propertyName))
    {
    return;
    }
    if (PropertyChanged != null)
    {
    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    }
    }
    -----------------------------------------------------------------------------------------------------------------------------------
    3)CustomItemsControl.cs
    ----------------------------
    using Telerik.Windows.Controls;
    using System.Windows;
    using System.Collections.Specialized;
    using System.Diagnostics;
    using System.Windows.Controls;
    namespace TreeViewDragDropBug
    {
    public class MyControl : HeaderedItemsControl
    {
    protected override bool IsItemItsOwnContainerOverride(object item)
    {
    return item is MyControl;
    }
    private static int uid;
    protected override System.Windows.DependencyObject GetContainerForItemOverride()
    {
    var result = new MyControl();
    result.Tag = uid++;
    return result;
    }
    protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
    {
    Debug.WriteLine(string.Format("{0} @ {1}: {2} - {3}",
    e.Action,
    this.DataContext,
    e.OldItems != null ? e.OldItems[0] : null,
    e.NewItems != null ? e.NewItems[0] : null));
    base.OnItemsChanged(e);
    }
    protected override void ClearContainerForItemOverride(DependencyObject element, object item)
    {
    Debug.WriteLine(string.Format("Cle @ {0}: ({1}, {2})",
    this.DataContext,
    (element as Control).Tag,
    item));
    base.ClearContainerForItemOverride(element, item);
    }
    protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
    {
    Debug.WriteLine(string.Format("Pre @ {0}: ({1}, {2})",
    this.DataContext,
    (element as Control).Tag,
    item));
    base.PrepareContainerForItemOverride(element, item);
    }
    }
    }


    Thanks,
    Gaurang
  2. Gaurang
    Gaurang avatar
    29 posts
    Member since:
    Dec 2010

    Posted 12 Sep 2011 Link to this post

    Hi,

    I solve this problem Using RadTreeView  MouseLeftButtonUpevent insted of SelectionChange even of RadTreeView.

    Thanks,
    Gaurang
  3. DevCraft banner
Back to Top