How to retrieve all items in listbox and put them to a string?

753
4
05-19-2020 12:15 PM
helenchu
Occasional Contributor II

Hi all,

I have 

<ListBox x:Name="lstAllAcct" Grid.Row="1"  ItemsSource="{Binding ExcelData}"/>

<Button x:Name="btnNewSelection" Command="{Binding Path=NewSelectionCmd}">

{Binding ExcelData} is to get accounts from excel sheet to my listbox : done

Now when button btnNewSelection is click I want to get all accounts in that listbox to a string (to construct my query statement)

The code behind for the button:

private RelayCommand _newSelectionCmd;
public ICommand NewSelectionCmd
{
get
{
if (_newSelectionCmd == null)

_newSelectionCmd = new RelayCommand(() => DoSearch(), () => true);

return _newSelectionCmd;
}

private void DoSearch ()
{

string strAllAccount =""

(how to I get all items in my listbox to strAllAccount?)
}

Your help is very much appreciated.

0 Kudos
4 Replies
GKmieliauskas
Esri Regular Contributor

Hi Helen,

The answer depends on your ExcelData property. If your ExcelData property is type of List<string>, then 

string strAllAccount = string.Join("', '", ExcelData.ToArray());

0 Kudos
helenchu
Occasional Contributor II

Hi, in my case, after loading ExcelData, I move the item(s) around to other listboxes depends on the type.

So let me rephrase my question, 

below is my listbox

<ListBox x:Name="lslSubjAcct" />

Because I wrote codes to load data to lstSubjAcct listbox  in a button click event therefore I have no ItemsSource binding in my xaml file.

Now I need to get all items in my lstSubjAcct listbox to a string in the viewmodel, how do I do that ?  Thank you!

private void BtnAddSubject_Click(object sender, RoutedEventArgs e)
{
string currentItemText;
int currentItemIndex;
List<string> myDataList = new List<string>();

foreach (object item in lstAllAcct.Items)
myDataList.Add(item as string);

// Find the right item and it's value and index
currentItemText = lstAllAcct.SelectedValue.ToString();
currentItemIndex = lstAllAcct.SelectedIndex;
lslSubjAcct.Items.Add(currentItemText);
if (myDataList != null)
{
myDataList.RemoveAt(currentItemIndex);
}
// Refresh data binding
ApplyDataBinding(myDataList);

}
private void ApplyDataBinding(List<string> myNewDataList)
{
lstAllAcct.ItemsSource = null;
// Bind ArrayList with the ListBox
lstAllAcct.ItemsSource = myNewDataList;

}

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Hi Helen,

 It appears that you are not using MVVM which is the programming pattern used by Dockpanes in the Pro SDK.  MVVM is programming pattern not specific to the Pro SDK and is used in many Enterprise implementations for WPF desktop applications.  If you look at this question: https://community.esri.com/message/922908-re-access-listview-inside-dockpane?commentID=922908#commen...  it explains the issue that you are running into.  In order to implement a button (or command) for example you have to use the ICommand pattern by implementing a public property that implements the ICommand Interface in the ViewModel.  Remember all your code goes into the ViewModel not in the View's (xaml.cs) code behind file.  So don't double click a button control to implement an 'OnClick' handler, there are no handlers in MVVM.   I attached a 2.5 sample that implements a listbox, a textbox, and a set of buttons in MVVM.  Remember the the View (your XAML) and the ViewModel (your code in the viewmodel.cs file) are linked by using Data Binding.  So the public properties in your ViewModel code have to have a corresponding property with {Binding property_name}.  In the ViewModel code you would use either SetProperty or NotifyPropertyChanged in order to make a change (with is normally in the setter of the property) to either your list or any other property (see for example the ListItem class in my sample). Using  SetProperty or NotifyPropertyChanged will ensure that the UI (your XAML) is notified that your data (properties) has changed and the UI can now update the control on the display. 

LeLuong
New Contributor III

Thank you very much Wolfgang for the sample codes and specially spending time to explain them in details.  I'm studying them right now.  Hopefully 1 day I will be more comfortable with coding in MVVM.  Your help is very much appreciated.

0 Kudos