Select to view content in your preferred language

Access listview inside dockpane

2510
5
Jump to solution
04-16-2020 02:09 PM
helenchu
Frequent Contributor

Hi,

I'm a new with both C# and Pro SDK.  I have to convert my vb.net ArcMap addin to Pro.  I'm taking baby steps here so please bear with me.   Today I'm trying to write codes for the addin for user to open an excel file and import a column to a list view which I put inside a dockpane.  

I got errors at 

lstviewAllAcct.View = View.Details;  //error: View does not exist in the current context
lstviewAllAcct.GridLines = true; //error: 'ListView' does not contain a definition for 

GridLines and no accessible extension method
lstviewAllAcct.FullRowSelect = true; //error: same as above

How to I access the properties of my listview? Thanks for your help.

using ArcGIS.Desktop.Catalog;
using Microsoft.Win32;
using OfficeOpenXml; //reference EPPlus (excelpackage)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace ProAppApril92
{
    /// <summary>
    /// Interaction logic for Dockpane1View.xaml
    /// </summary>
    public partial class Dockpane1View : UserControl
    {
        public Dockpane1View()
        {
            InitializeComponent();
        }

        private void BtnOpenExcelFile_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = @"c:\";
            openFileDialog1.Filter = "XLSX Files (*.xlsx)|*.xlsx|XLS Files (*.xls)|*.xls|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.ShowDialog();

            var package = new ExcelPackage();
            var workbook = package.Workbook;
            if (workbook != null)
            {
                var sheet = workbook.Worksheets[1];
                var firstColumnRows = sheet.Cells["A1:A"]; 
                object cell;
                lstviewAllAcct.View = View.Details;
                lstviewAllAcct.GridLines = true;
                lstviewAllAcct.FullRowSelect = true;
            }

            else
                System.Windows.MessageBox.Show(openFileDialog1.Title);
        }
    }

  
}
0 Kudos
1 Solution

Accepted Solutions
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Hi Helen,

 I am attaching a sample project for you that demonstrates how to populate a listbox with two columns using the first two columns of a spreadsheet.  I think this is a useful sample and I will add it with the next release to the community samples.  The add-in looks for a .csv file that I included in the project source as well (it's called Meteorites_UK.csv).  You can read any file that can be opened with Excel with this code but you have to match the sheet name and column count.  To run the add-in, open the "Show Spreadsheet" dockpane, then use the "import csv" button to browse to Meteorites_UK.csv.  

Once you click "Open" on the input selection dialog you see the listbox getting populated.

If you look at ShowSpreadsheet.xaml you find that the listview is bound to two properties: SpreadSheetRows for the ItemsSource and SelectedSpreadSheetRow for the SelectedItem attributes.  If you then look in the code behind file ShowSpreadsheetViewModel.cs you see the appropriate properties that match these 'bindings': 

public ObservableCollection<SpreadSheetColumns> SpreadSheetRows‍

and

 public SpreadSheetColumns SelectedSpreadSheetRow‍

When you look at these properties you will notice that both are using 'SetProperty' in the setter. 'SetProperty' is important because it initiates the notification mechanism that notifies the UI when one of these properties is changed (or 'set').  This mechanism is called data binding and you can read up on it if you search for 'WPF data binding'.  

Anyways I hope this sample helps.  

View solution in original post

5 Replies
Wolf
by Esri Regular Contributor
Esri Regular Contributor

When you implement a dockpane using the Pro SDK it supports MVVM (Model - View - ViewModel) out of box.  You can check out this video (starting at minute 20:00 of the video) to get an overview of MVVM in the Pro SDK: https://www.youtube.com/watch?v=5PgaeZycWXc

What this means is that the Pro SDK Item Template for DockPanes creates multiple files in support of MVVM for you.  These files are used for:

  1. The View (or UI): Dockpane1.xaml (and included with the XAML is the usually hidden Dockpane1.cs file containing the partial class implementation of the Dockpane1View / UserControl class).  If you make any changes to the View you ONLY make these changes in XAML.  You would not make any changes in the code behind for the View (Dockpane1View.cs file) when you are coding using the MVVM programming pattern .  
  2. The ViewModel (or business logic, code behind): Dockpane1ViewModel.cs contains the actual business logic and the properties to exchange data at runtime with the View using data binding.  In MVVM this is were all code (business logic) goes, including the properties (which must be public) that are 'data bound' to the View (UI implemented in XAML) 

Looking at your code snippet I see that you mixed MVVM with a non MVVM pattern.  To implemented a command using the MVVM pattern, you have to utilize what is known as the ICommand pattern.  There a plenty of examples under the Pro SDK community sample github repo, but you can search for BrowseCommand in the UploadItem sample and you find the XAML implementation here:

arcgis-pro-sdk-community-samples/UploadItem.xaml at f5f9dda18efa173a56d128f3ea64ca34ac3f68a9 · Esri/... 

and the business logic for the BrowseCommand button is here:

arcgis-pro-sdk-community-samples/UploadItemViewModel.cs at f5f9dda18efa173a56d128f3ea64ca34ac3f68a9 ... 

helenchu
Frequent Contributor

Thank you very much.  I'll work on it and keep you posted.  I still can't wrap my brain around this.  Addin for Arcmap was a lot more straight forward to me.  

0 Kudos
helenchu
Frequent Contributor

Hi Wolfgang,

I've been studying the sample you suggested.  I understand it a bit more but I'm still pvery much in dark.  Hopefully you can shed some more light on me.  

I find case of textbox, it uses <TextBox Text="{Binding ItemID}"> in .xaml file, to bind the data returned from the codes in Dockpane1ViewModel.cs .

My case I have to use listview to list all lines returned from excel.  Which property of listview should I use for binding in .xaml file ?

And many properties like  View = View.Details; GridLines = true; FullRowSelect = true are not available in .xaml too.  

Your help is greatly appreciated.

---codes from my arcmap addin---------------

Dim workbook = package.Workbook
If workbook IsNot Nothing Then
Dim sheet = workbook.Worksheets(1)
Dim firstColumnRows = sheet.Cells("A1:A") ' Loop through rows in the first column, get values based on offset
Dim cell As Object
myForm.lstAllAcct.View = View.Details
myForm.lstAllAcct.Clear()

myForm.lstAllAcct.Columns.Add("Account Number", 122)

For Each cell In firstColumnRows
Dim column1CellValue = cell.GetValue(Of String)()
lvItem = myForm.lstAllAcct.Items.Add(Trim(column1CellValue.ToString))
Next
End If

-----------------------------------------------------------

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Hi Helen,

 I am attaching a sample project for you that demonstrates how to populate a listbox with two columns using the first two columns of a spreadsheet.  I think this is a useful sample and I will add it with the next release to the community samples.  The add-in looks for a .csv file that I included in the project source as well (it's called Meteorites_UK.csv).  You can read any file that can be opened with Excel with this code but you have to match the sheet name and column count.  To run the add-in, open the "Show Spreadsheet" dockpane, then use the "import csv" button to browse to Meteorites_UK.csv.  

Once you click "Open" on the input selection dialog you see the listbox getting populated.

If you look at ShowSpreadsheet.xaml you find that the listview is bound to two properties: SpreadSheetRows for the ItemsSource and SelectedSpreadSheetRow for the SelectedItem attributes.  If you then look in the code behind file ShowSpreadsheetViewModel.cs you see the appropriate properties that match these 'bindings': 

public ObservableCollection<SpreadSheetColumns> SpreadSheetRows‍

and

 public SpreadSheetColumns SelectedSpreadSheetRow‍

When you look at these properties you will notice that both are using 'SetProperty' in the setter. 'SetProperty' is important because it initiates the notification mechanism that notifies the UI when one of these properties is changed (or 'set').  This mechanism is called data binding and you can read up on it if you search for 'WPF data binding'.  

Anyways I hope this sample helps.  

helenchu
Frequent Contributor

Thank you so very much Wolfgang. 

0 Kudos