locked
Display more than one selected listview item in a textbox RRS feed

  • Question

  • Hi,

    How can I display the multiple selected items of a listview into a textbox?

    Currently, I'm able to display one selected value only. I want to be able to display more than one values in the textbox based on the selected software.

    Here's the image of the listview and textbox:

    Edit:

    I've tried to add the following code at the code-behind for the software listview selected change event:

    char[] charsToTrim = { ',', ' ' };
    if (softwareListView.SelectedItems.Count != 0)
    {
        foreach (Software item in softwareListView.SelectedItems)
        {
            tbIncSoft.Text += item.VmIncSoftware + ", ";
        }
        tbIncSoft.Text = tbIncSoft.Text.TrimEnd(charsToTrim);
    } 
    But I still can't get the textbox text to display the selected items. 

    Any help is greatly appreciated.

    Thanks.


    • Edited by Dewi Abdullah Wednesday, September 4, 2013 7:24 AM Edit
    Wednesday, September 4, 2013 4:34 AM

Answers

  • Hi,

    Use MVVM Pattern. you can achieve this task with ease. just developed a short project.

    View.xaml

    <StackPanel x:Name="LstPanel">
                    <TextBox IsReadOnly="True" TextWrapping="NoWrap" AcceptsReturn="True" Height="200" Text="{Binding DisplayString}"/>
                    <ListView ItemsSource="{Binding Softwares}" SelectionChanged="ListView_SelectionChanged" SelectionMode="Multiple"/>
                </StackPanel>

    View.xaml.cs

    public partial class MainWindow : Window
        {
            private SoftwaresViewModel _softwaresViewModel;
            private static ObservableCollection<string> _selectedSoftwares;
            public MainWindow()
            {
                InitializeComponent();
                this.DataContext = new SoftwaresViewModel();
                _softwaresViewModel = this.DataContext as SoftwaresViewModel;
                _selectedSoftwares = new ObservableCollection<string>();
            }
    
            private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (e.RemovedItems.Count > 0)
                {
                    _selectedSoftwares = new ObservableCollection<string>(_selectedSoftwares.Where(s => s != Convert.ToString(e.RemovedItems[0])));
                    _softwaresViewModel.SelectedSoftwares = _selectedSoftwares;
                }
                if (e.AddedItems.Count > 0)
                {
                    _selectedSoftwares.Add(Convert.ToString(e.AddedItems[0]));
                    _softwaresViewModel.SelectedSoftwares = _selectedSoftwares;
                }
            }
        }

    ViewModel.cs

      public class SoftwaresViewModel : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            /// <summary>
            /// Displays the selected softwares in Textbox
            /// </summary>
            private string _displayString;
            public string DisplayString
            {
                get { return _displayString; }
                set
                {
                    this._displayString = value;
                    OnPropertyChanged("DisplayString");
                }
            }
    
            /// <summary>
            /// Collection of selected software from the list view
            /// </summary>
            private ObservableCollection<string> _selectedSoftwares;
            public ObservableCollection<string> SelectedSoftwares
            {
                get { return this._selectedSoftwares; }
                set
                {
                    this._selectedSoftwares = value;
                    DisplaySelectedSofware();
                    OnPropertyChanged("SelectedSoftwares");
                }
            }
    
            /// <summary>
            /// Collection of all softwares to be binded in the list
            /// </summary>
            private List<string> _softwares = new List<string> { "Microsoft Visual Studio", "Microsoft Office", "Sharepoint", "Windows 8", "Windows Azure", "Silverlight" };
            public List<string> Softwares
            {
                get { return this._softwares; }
            }
    
            /// <summary>
            /// Constructor
            /// </summary>
            public SoftwaresViewModel()
            {
                this.SelectedSoftwares = new ObservableCollection<string>();
            }
    
            /// <summary>
            /// Function to format the selected software to display in the textbox
            /// </summary>
            private void DisplaySelectedSofware()
            {
                var stringSelectedSoftwares = string.Empty;
                foreach (var software in this._selectedSoftwares)
                {
                    stringSelectedSoftwares = stringSelectedSoftwares + "\n" + software;
                }
                this.DisplayString = stringSelectedSoftwares;
            }
    
            public void OnPropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }


    srithar


    • Edited by Srithar Wednesday, September 4, 2013 11:42 AM
    • Marked as answer by Jamles HezModerator Tuesday, September 10, 2013 6:52 AM
    Wednesday, September 4, 2013 11:38 AM
  • Hi,

    If the items are selected it will go into e.AddedItems List. if the items is selected first and then unselected it will go into e.RemovedItems List. 

    so if the items are Unselected after selected the first part of if will execute and update the Collection "SelectedSoftwares" in the viewmodel

    if (e.RemovedItems.Count > 0)
    {
    //Removes the Unselected items after being selected from the List and Update the viewmodel
        _selectedSoftwares = new ObservableCollection<string>(_selectedSoftwares.Where(s => s != Convert.ToString(e.RemovedItems[0])));
        _softwaresViewModel.SelectedSoftwares = _selectedSoftwares;
    }

    so if the items are selected the second part of if will execute and update the Collection "SelectedSoftwares" in the viewmodel.

    if (e.AddedItems.Count > 0)
    {   
    //Adds the selected item into the Collection and Update the Viewmodel
         _selectedSoftwares.Add(Convert.ToString(e.AddedItems[0]));
        _softwaresViewModel.SelectedSoftwares = _selectedSoftwares;
    }


    srithar

    Thursday, September 5, 2013 6:03 AM

All replies

  • Hi,

    Use MVVM Pattern. you can achieve this task with ease. just developed a short project.

    View.xaml

    <StackPanel x:Name="LstPanel">
                    <TextBox IsReadOnly="True" TextWrapping="NoWrap" AcceptsReturn="True" Height="200" Text="{Binding DisplayString}"/>
                    <ListView ItemsSource="{Binding Softwares}" SelectionChanged="ListView_SelectionChanged" SelectionMode="Multiple"/>
                </StackPanel>

    View.xaml.cs

    public partial class MainWindow : Window
        {
            private SoftwaresViewModel _softwaresViewModel;
            private static ObservableCollection<string> _selectedSoftwares;
            public MainWindow()
            {
                InitializeComponent();
                this.DataContext = new SoftwaresViewModel();
                _softwaresViewModel = this.DataContext as SoftwaresViewModel;
                _selectedSoftwares = new ObservableCollection<string>();
            }
    
            private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (e.RemovedItems.Count > 0)
                {
                    _selectedSoftwares = new ObservableCollection<string>(_selectedSoftwares.Where(s => s != Convert.ToString(e.RemovedItems[0])));
                    _softwaresViewModel.SelectedSoftwares = _selectedSoftwares;
                }
                if (e.AddedItems.Count > 0)
                {
                    _selectedSoftwares.Add(Convert.ToString(e.AddedItems[0]));
                    _softwaresViewModel.SelectedSoftwares = _selectedSoftwares;
                }
            }
        }

    ViewModel.cs

      public class SoftwaresViewModel : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            /// <summary>
            /// Displays the selected softwares in Textbox
            /// </summary>
            private string _displayString;
            public string DisplayString
            {
                get { return _displayString; }
                set
                {
                    this._displayString = value;
                    OnPropertyChanged("DisplayString");
                }
            }
    
            /// <summary>
            /// Collection of selected software from the list view
            /// </summary>
            private ObservableCollection<string> _selectedSoftwares;
            public ObservableCollection<string> SelectedSoftwares
            {
                get { return this._selectedSoftwares; }
                set
                {
                    this._selectedSoftwares = value;
                    DisplaySelectedSofware();
                    OnPropertyChanged("SelectedSoftwares");
                }
            }
    
            /// <summary>
            /// Collection of all softwares to be binded in the list
            /// </summary>
            private List<string> _softwares = new List<string> { "Microsoft Visual Studio", "Microsoft Office", "Sharepoint", "Windows 8", "Windows Azure", "Silverlight" };
            public List<string> Softwares
            {
                get { return this._softwares; }
            }
    
            /// <summary>
            /// Constructor
            /// </summary>
            public SoftwaresViewModel()
            {
                this.SelectedSoftwares = new ObservableCollection<string>();
            }
    
            /// <summary>
            /// Function to format the selected software to display in the textbox
            /// </summary>
            private void DisplaySelectedSofware()
            {
                var stringSelectedSoftwares = string.Empty;
                foreach (var software in this._selectedSoftwares)
                {
                    stringSelectedSoftwares = stringSelectedSoftwares + "\n" + software;
                }
                this.DisplayString = stringSelectedSoftwares;
            }
    
            public void OnPropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }


    srithar


    • Edited by Srithar Wednesday, September 4, 2013 11:42 AM
    • Marked as answer by Jamles HezModerator Tuesday, September 10, 2013 6:52 AM
    Wednesday, September 4, 2013 11:38 AM
  • Hi Srithar,

    Thanks for the example. It works great! 

    Do you mind explaining the following lines of code? 

    if (e.RemovedItems.Count > 0)
    {
        _selectedSoftwares = new ObservableCollection<string>(_selectedSoftwares.Where(s => s != Convert.ToString(e.RemovedItems[0])));
        _softwaresViewModel.SelectedSoftwares = _selectedSoftwares;
    }
    if (e.AddedItems.Count > 0)
    {   _selectedSoftwares.Add(Convert.ToString(e.AddedItems[0]));
        _softwaresViewModel.SelectedSoftwares = _selectedSoftwares;
    }
    Thanks.

    Wednesday, September 4, 2013 5:04 PM
  • Hi,

    If the items are selected it will go into e.AddedItems List. if the items is selected first and then unselected it will go into e.RemovedItems List. 

    so if the items are Unselected after selected the first part of if will execute and update the Collection "SelectedSoftwares" in the viewmodel

    if (e.RemovedItems.Count > 0)
    {
    //Removes the Unselected items after being selected from the List and Update the viewmodel
        _selectedSoftwares = new ObservableCollection<string>(_selectedSoftwares.Where(s => s != Convert.ToString(e.RemovedItems[0])));
        _softwaresViewModel.SelectedSoftwares = _selectedSoftwares;
    }

    so if the items are selected the second part of if will execute and update the Collection "SelectedSoftwares" in the viewmodel.

    if (e.AddedItems.Count > 0)
    {   
    //Adds the selected item into the Collection and Update the Viewmodel
         _selectedSoftwares.Add(Convert.ToString(e.AddedItems[0]));
        _softwaresViewModel.SelectedSoftwares = _selectedSoftwares;
    }


    srithar

    Thursday, September 5, 2013 6:03 AM
  • Hi Srithar,

    Thanks for your explanation. I understand better now. 

    I've another complication.

    When I select 2 different item, it displayed 2 items of the second clicked item instead. How can I overcome this? 

    Here's a code snippet of the "list of software" listview selectedChanged:

    foreach (var software in softwareListView.SelectedItems)
    {
         softwareName = (software as ItemDetails).VmIncSoftware;
         selectedSoftware.VmIncSoftware = softwareName;
         _selectedSoftware.Add(selectedSoftware);
    }
    this.selectedSoftwareListView.ItemsSource = _selectedSoftware;


    Thanks.

    Thursday, September 5, 2013 8:03 AM
  • Hi,

    Clear the List first and then add the items. otherwise duplicates will be added.

    _selectedSoftware.Clear();
    foreach (var software in softwareListView.SelectedItems)
    {
         softwareName = (software as ItemDetails).VmIncSoftware;
         selectedSoftware.VmIncSoftware = softwareName;
         _selectedSoftware.Add(selectedSoftware);
    }
    this.selectedSoftwareListView.ItemsSource = _selectedSoftware;


    srithar

    Thursday, September 5, 2013 9:04 AM