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

Change the Main window Code to MVVM

$
0
0

HI ,

Im Using the following code which is working well,my question is how to use it with MVVM.

Ie the main window don't have anything and and all the code should be in the model view.

I read about MVVM and the problem here is how to call to the event from the model view instead the user control

 public partial class MainWindow : Window
    {
        public ObservableCollection<User> _UsersList = new ObservableCollection<User>();
        private readonly Dictionary<string, string> _mapping = new Dictionary<string, string>();
        private const string DRAG_SOURCE = "DragSource";

        public MainWindow()
        {

            InitializeComponent();
            _UsersList.Add(new User { Name = "Jhon" });
            _UsersList.Add(new User { Name = "Mike" });
            _UsersList.Add(new User { Name = "Alex" });
            _UsersList.Add(new User { Name = "Darl" });

            CollectionViewSource source = this.Resources["source"] as CollectionViewSource;
            source.Source = _UsersList;
            ListBox.SelectionChanged += listbox_SelectionChanged;

            DataObject.AddCopyingHandler(text1, DragCopy);
            DataObject.AddCopyingHandler(text2, DragCopy);
        }

        public ObservableCollection<User> UserList
        {
            get { return _UsersList; }
        }


        private void listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 1)
            {
                if (ListBox.SelectedItems.Count > 0)
                {
                    var mySelectedItem = ListBox.SelectedItem as User;
                    if (mySelectedItem != null)
                    {
                        DragDrop.DoDragDrop(ListBox, mySelectedItem,
                        DragDropEffects.Copy | DragDropEffects.Move);

                    }
                }
            }
        }

        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))
            {

                DataObject data = e.Data as DataObject;
                User user = data.GetData(typeof(User)) as User;
                textbox.Tag = user;

                var name = user.Name;
                textbox.Text += name;
                textbox.Focus();
                textbox.CaretIndex = textbox.Text.Length;
                e.Handled = true;
                var remove = _UsersList.Remove((User)ListBox.SelectedItem);

                if (!_mapping.ContainsKey(textbox.Name))
                    _mapping.Add(textbox.Name, name);
            }

            e.Handled = true;
        }

        private void DropText_PreviewDragOver(object sender, DragEventArgs e)
        {
            e.Handled = true;
        }

        private void ListBox_Drop(object sender, DragEventArgs e)
        {
            DataObject data = e.Data as DataObject;
            if (data != null)
            {
                User user = data.GetData(typeof(User)) as User;
                if (user != null && !_UsersList.Contains(user))
                    _UsersList.Add(user);
            }


            TextBox txtBox = e.Data.GetData(DRAG_SOURCE) as TextBox;
            if (txtBox != null)
            {
                if (_mapping.ContainsKey(txtBox.Name))
                    _mapping.Remove(txtBox.Name);
                txtBox.Dispatcher.BeginInvoke((Action)(() => { txtBox.Text = string.Empty; }), System.Windows.Threading.DispatcherPriority.Normal);
            }
            e.Handled = true; 
       }

        private void DragCopy(object sender, DataObjectCopyingEventArgs e)
        {
            if (e.IsDragDrop)
            {
                e.CancelCommand();
                TextBox txtBox = sender as TextBox;
                if (txtBox != null && txtBox.Tag != null)
                {
                    DataObject dataObject = new DataObject(txtBox.Tag);
                    dataObject.SetData(DRAG_SOURCE, txtBox);
                    DragDrop.DoDragDrop(sender as DependencyObject, dataObject, DragDropEffects.Move | DragDropEffects.Copy);
                }
                e.Handled = true;
            }
        }

        private void DropText_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox txtBox = sender as TextBox;
            if (txtBox.Text == string.Empty)
            {
                User user = txtBox.Tag as User;

                if(user != null && !_UsersList.Contains(user))
                    _UsersList.Add(user);

                if (_mapping.ContainsKey(txtBox.Name))
                    _mapping.Remove(txtBox.Name);
            }
        }
    }

XAML

<ListBox x:Name="ListBox" HorizontalAlignment="Left" Height="115"
                  VerticalAlignment="Top" Width="150" ItemsSource="{Binding Source={StaticResource source}}" 
                 DisplayMemberPath="Name"
                 AllowDrop="True" Drop="ListBox_Drop" />
<Window.Resources><CollectionViewSourcex:Key="source"></CollectionViewSource></Window.Resources>
<TextBox x:Name="text1"  
                  AcceptsReturn="True"
                  AllowDrop="True" 
                  PreviewDragEnter="DropText_PreviewDragEnter"
                  PreviewDrop="DropText_PreviewDrop"
                  PreviewDragOver="DropText_PreviewDragOver"
                  TextChanged="DropText_TextChanged"
                  Grid.Column="1"
                  HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" 
                  VerticalAlignment="Top" Width="120"/><TextBox x:Name="text2"  
                  AcceptsReturn="True"
                  AllowDrop="True" 
                  PreviewDragEnter="DropText_PreviewDragEnter"
                  PreviewDrop="DropText_PreviewDrop"
                  PreviewDragOver="DropText_PreviewDragOver"
                  TextChanged="DropText_TextChanged"
                  Grid.Column="1"
                  HorizontalAlignment="Left" Height="23" TextWrapping="Wrap"
                  VerticalAlignment="Top" Width="120"/>


Viewing all articles
Browse latest Browse all 18858

Trending Articles



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