Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Using Drag and &Dr with textBox

$
0
0

HI


I have read recent post in the MSDN and I found something very similar :) for what I need.

I need to create D&D from listBox to textBox and I found this code which is working 4 me but I have additional  questions


1.When user does D&D to some textBox and than regret and want to delete the textBox value, How should he do that?

(I see that there is option to drag back but if he want to mark the value and press on the delete button on the keyboard,currently nothing happen)

2. How should he do the the mapping table ,for instance you need to keep mapping at the end of the process for the value(User name like "Person01")

and the textBoxes (like x:Name="textBox1") (that the value was dragged too,how can we do that?

Below is the code which is working for me :-)

Thank you & Best Regards,

Miley


<UserControl x:Class="DD.Views.ModelView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             d:DesignHeight="300" d:DesignWidth="300"><Grid><StackPanel Orientation="Vertical" Visibility="Visible"><ListBox x:Name="LstUsers" Height="115" Width="150" ItemsSource="{Binding UsersCollectionView}" SelectionChanged="listbox_SelectionChanged" AllowDrop="True" PreviewDrop="ListBox_PreviewDrop"><ListBox.ItemTemplate><DataTemplate><TextBlock Text="{Binding Name}"/></DataTemplate></ListBox.ItemTemplate></ListBox><TextBox x:Name="textbox1"  
                 AllowDrop="True" 
                 PreviewMouseDown="DropText_PreviewMouseDown"
                 PreviewDragEnter="DropText_PreviewDragEnter"
                 PreviewDrop="DropText_PreviewDrop"
                 AcceptsReturn="True"
                 TextWrapping="Wrap"
                 Height="50"
                 Width="120"/><TextBox x:Name="textbox2"  
                 AllowDrop="True" 
                 PreviewMouseDown="DropText_PreviewMouseDown"
                 PreviewDragEnter="DropText_PreviewDragEnter"
                 PreviewDrop="DropText_PreviewDrop"
                 AcceptsReturn="True"
                 TextWrapping="Wrap"
                 Height="50"
                 Width="120"/></StackPanel></Grid></UserControl>

The model view.

internal class ModelViewModel
    {
        private ObservableCollection<User> _UsersList;
        private ICollectionView usersCollectionView;
        public ICollectionView UsersCollectionView
        {
            get
            {
                return usersCollectionView;
            }
        }
        public ObservableCollection<User> UserList
        {
            get
            {
                return _UsersList;
            }
        }

        public ModelViewModel()
        {
            _UsersList = new ObservableCollection<User>();
            _UsersList.Add(new User { Name = "Person01" });
            _UsersList.Add(new User { Name = "Person02" });
            _UsersList.Add(new User { Name = "Person03" });
            usersCollectionView = CollectionViewSource.GetDefaultView(_UsersList);
            UsersCollectionView.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
        }
    }
The view
public partial class ModelView : UserControl
    {
        private const string DRAG_SOURCE = "DragSource";
        private static ModelViewModel ModelViewInstance;
        public MyUserControl()
        {
            InitializeComponent();
            ModelViewInstance = new ModelViewModel();
            this.DataContext = ModelViewInstance;
        }

        private void DropText_PreviewDragEnter(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.None;
        }


        private void DropText_PreviewDrop(object sender, DragEventArgs e)
        {
            var textbox = (TextBox)sender;

            if (!(textbox.Text.Length > 0))
            {
                string name = e.Data.GetData(DataFormats.StringFormat).ToString();
                textbox.Text += name;
                textbox.Focus();
                textbox.CaretIndex = textbox.Text.Length;
                e.Handled = true;
                ModelViewInstance.UserList.Remove(LstUsers.SelectedItem as User);
            }
            else
            {
                e.Handled = true;
            }
        }


        private void ListBox_PreviewDrop(object sender, DragEventArgs e)
        {
            object dragSource = e.Data.GetData(DRAG_SOURCE);
            if (dragSource != null && dragSource is TextBox)
            {
                (dragSource as TextBox).Text = String.Empty;
            }
            if (!String.IsNullOrEmpty(e.Data.GetData(DataFormats.StringFormat).ToString()) && dragSource is TextBox)
            {
                var newUser = new User { Name = e.Data.GetData(DataFormats.StringFormat).ToString() };
                ModelViewInstance.UserList.Add(newUser);
            }
            else
            {
                e.Handled = true;
            }
        }

        private void DropText_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            var textBox = (TextBox)sender;
            if (textBox != null)
            {
                var dataObject = new DataObject((textBox).Text);
                dataObject.SetData("DragSource", sender);
                DragDrop.DoDragDrop(textBox, dataObject, DragDropEffects.Copy | DragDropEffects.Move);
            }
        }

        private void listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (sender is ListBox)
            {
                var listBox = sender as ListBox;
                if (e.AddedItems.Count == 1)
                {
                    if (listBox.SelectedItem != null)
                    {
                        var mySelectedItem = listBox.SelectedItem as User;
                        if (mySelectedItem != null)
                        {
                            DragDrop.DoDragDrop(listBox, mySelectedItem.Name, DragDropEffects.Copy | DragDropEffects.Move);
                        }
                    }
                }
            }
            else
                return;
        }
    }








Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>