Im having list box and there is option to D&D from list box to text box
Currently i don't know how to implement the scenario when the user is regret and want to delete the value from the text box.
There is two option of deleting the value i guess.
1. Mark the cursor on the end of text box value and delete it with the arrow (<-) character by character
2. mark all the value with the mouse and delete it by pressing on the delete button on the keyboard
I know that there is option to use the following event but this is very complicated I guess since if user use the first option to delete...
TextChanged="textbox_TextChanged"
(when the value is deleted the list view should be updated)
I'm Stuck :(,
I need your help please ...
if this is not the right forum ,please let me know.
Following is the code
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><StackPanelOrientation="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><TextBlockText="{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>
View model
internalclassModelViewModel{privateObservableCollection<User>_UsersList;privateICollectionView usersCollectionView;publicICollectionViewUsersCollectionView{get{return usersCollectionView;}}publicObservableCollection<User>UserList{get{return_UsersList;}}publicModelViewModel(){_UsersList=newObservableCollection<User>();_UsersList.Add(newUser{Name="Person01"});_UsersList.Add(newUser{Name="Person02"});_UsersList.Add(newUser{Name="Person03"}); usersCollectionView =CollectionViewSource.GetDefaultView(_UsersList);UsersCollectionView.SortDescriptions.Add(newSortDescription("Name",ListSortDirection.Ascending));}
The View
publicpartialclassModelView:UserControl{privateconststring DRAG_SOURCE ="DragSource";privatestaticModelViewModelModelViewInstance;publicMyUserControl(){InitializeComponent();ModelViewInstance=newModelViewModel();this.DataContext=ModelViewInstance;}privatevoidDropText_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.SelectedItemasUser);}else{ e.Handled=true;}}privatevoidListBox_PreviewDrop(object sender,DragEventArgs e){object dragSource = e.Data.GetData(DRAG_SOURCE);if(dragSource !=null&& dragSource isTextBox){(dragSource asTextBox).Text=String.Empty;}if(!String.IsNullOrEmpty(e.Data.GetData(DataFormats.StringFormat).ToString())&& dragSource isTextBox){var newUser =newUser{Name= e.Data.GetData(DataFormats.StringFormat).ToString()};ModelViewInstance.UserList.Add(newUser);}else{ e.Handled=true;}}privatevoidDropText_PreviewMouseDown(object sender,MouseButtonEventArgs e){var textBox =(TextBox)sender;if(textBox !=null){var dataObject =newDataObject((textBox).Text); dataObject.SetData("DragSource", sender);DragDrop.DoDragDrop(textBox, dataObject,DragDropEffects.Copy|DragDropEffects.Move);}}privatevoid listbox_SelectionChanged(object sender,SelectionChangedEventArgs e){if(sender isListBox){var listBox = sender asListBox;if(e.AddedItems.Count==1){if(listBox.SelectedItem!=null){var mySelectedItem = listBox.SelectedItemasUser;if(mySelectedItem !=null){DragDrop.DoDragDrop(listBox, mySelectedItem.Name,DragDropEffects.Copy|DragDropEffects.Move);}}}}}