Author Topic: PropertySetDefinition with a List data type  (Read 1983 times)

0 Members and 1 Guest are viewing this topic.

Jeff_M

  • King Gator
  • Posts: 4102
  • C3D user & customizer
PropertySetDefinition with a List data type
« on: May 08, 2024, 07:06:09 PM »
I have a tool for displaying and editing PropertySets which was working well until we added a definition with a List data type. I can get the list but I need to show it in a DataTable. I define the column as a DataType.List but have been unable to figure out how to actually use it in the table. When I add it when filling the table it just shows up as (Collection) but I need it to show the actual PropertySet current value. I had also thought that by using the List datatype the list would be shown as a dropdown to select another item in the list.
To save the List column data I use a Dictionary since there may be mor than one column of this type:
Code - C#: [Select]
  1.             var listDictionary = new Dictionary<string, List<string>>();
  2.  
As I loop through the definitions to create the columns I get the Name and the actual list of items:
Code - C#: [Select]
  1.  case Autodesk.Aec.PropertyData.DataType.List:
  2.      dtype = typeof(List<string>);
  3.      var thelist = (AEC.ListDefinition)prop.ListDefinitionId.Open(OpenMode.ForRead);
  4.      var items = thelist.GetListItems();
  5.      var dataList = new List<string>();
  6.      foreach (ObjectId item in items)
  7.      {
  8.          var j = (AEC.ListItem)item.Open(OpenMode.ForRead);
  9.          dataList.Add(j.Name);
  10.      }
  11.      listDictionary.Add(prop.Name, dataList);
  12.      break;
Then, when I get the actual PropertySet data assigned to an object fill the table rows:
Code - C#: [Select]
  1.                                 foreach (string s in GetPropDefs(property))
  2.                                 {
  3.                                     var d = propset.GetAt(propset.PropertyNameToId(s));
  4.                                     if (theTable.Columns[s].DataType == typeof(List<string>))
  5.                                     {
  6.                                         List<string> dd = listDictionary[s];
  7.                                         row[s] = dd; //How to display the actual value of s which is a member of the list?
  8.                                         continue;
  9.                                     }
  10.                                     row[s] = d; //This works for DataTypes int, double, string, and bool
  11.                                 }
  12.  
If I make the Column for the List as a string type, I can just assign the PropertySet Value, but then the list is ignored.

I'm drawing a blank on how to implement this. A Google search gives all kinds of answers on how to create the DataTable from a list but nothing on how to use a list with a DataType.List cell.

Jeff_M

  • King Gator
  • Posts: 4102
  • C3D user & customizer
Re: PropertySetDefinition with a List data type
« Reply #1 on: May 09, 2024, 03:59:18 PM »
A bit of headway made ... I changed the DataTable column to be a typeof Dictionary<string, List<string>> so I can put the current value as the Key and the List as the value to be used in the DataGrid. However, when the DataGrid columns are auto-generated, the column with the Dictionary is created as a DataGridTextColumn. I think that what I am after is to have that column be a DataGridComboBoxColumn set with the cell set to the value and the selected item set the Key of the dictionary.

I believe I'm on the right track here, just really struggling on how to accomplish it. WPF, MVVM, c#  :uglystupid2:

Hanauer

  • Mosquito
  • Posts: 11
Re: PropertySetDefinition with a List data type
« Reply #2 on: May 09, 2024, 04:37:17 PM »
I have been using DataGridViewComboBoxColumn and getting satisfactory results.
If you could post part of the code of the final solution you arrived at, I would be grateful.

Jeff_M

  • King Gator
  • Posts: 4102
  • C3D user & customizer
Re: PropertySetDefinition with a List data type
« Reply #3 on: May 15, 2024, 02:43:18 PM »
Okay, more headway made. I finally realized that I needed a new class (PropList) to hold the list and the current value for the object. I have not yet been able to get the Combobox to display the list or current value. The DataTable is created properly, with the column for the List type created as a PropList type. Then in the Window the DataGrid's ItemsSource is set to the ViewModel's property for the DataTable. All the columns fill with the correct values, except that PropList column. One of the solutions I found in my searches says that adding this to the XAML should fix it:
Code - XML: [Select]
  1.                 <DataGrid.Resources>
  2.                     <DataTemplate x:Key="dataItemCellTemplate">
  3.                         <ComboBox SelectedValue="{Binding DataContext.CurrentValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
  4.                                    ItemsSource="{Binding DataContext.Values}"/>
  5.                     </DataTemplate>
  6.                 </DataGrid.Resources>
  7.  

Where CurrentValue and Values are the 2 properties of PropList. Then include this in the code behind for the AutoGeneratingColumn event:
Code - C#: [Select]
  1.             if (e.PropertyType == typeof(PropList))
  2.             {
  3.                 var col = new DataGridTemplateColumn
  4.                 {
  5.                     CellTemplate = (DataTemplate)PropData.FindResource("dataItemCellTemplate"),
  6.                     CellEditingTemplate = (DataTemplate)PropData.FindResource("dataItemCellTemplate"),
  7.                     Header = e.PropertyName
  8.                 };
  9.                 e.Column = col;
  10.                 return;
  11.             }
  12.  
But something is still amiss and I am at a loss on where to look.

Jeff_M

  • King Gator
  • Posts: 4102
  • C3D user & customizer
Re: PropertySetDefinition with a List data type
« Reply #4 on: June 02, 2024, 03:10:11 PM »
I have tried a lot of the various solutions to this issue, none of which have worked. This one seemed to be the most promising. However I still cannot get the ComboBox populated with the list. Attached is a fully working test project which exhibits this. Any help getting this working will be much appreciated.

ChristopherF

  • Newt
  • Posts: 56
Re: PropertySetDefinition with a List data type
« Reply #5 on: June 22, 2024, 03:54:04 PM »
The attached zip file has the solution that I would use.

I tend to stay away from backend code, so I removed most of that and then used classes to control how the information is shown. I consider the drawing the "Model", so I end up skipping the ViewModel pattern and call everything Model instead. I've continued that bad habit in the attached code. Once a video finishes processing, I'll post a link to a video explaining what I've done and show all of the mistakes and dead ends I went down as well.

Video, warts and all: https://youtu.be/CyC9LPHNJ-g
« Last Edit: June 22, 2024, 04:20:16 PM by ChristopherF »

Jeff_M

  • King Gator
  • Posts: 4102
  • C3D user & customizer
Re: PropertySetDefinition with a List data type
« Reply #6 on: June 22, 2024, 05:41:39 PM »
Thanks, Christopher! This is a big help, both in helping me get this finished up and how I've been approaching it wrong from the start  :|

Jeff_M

  • King Gator
  • Posts: 4102
  • C3D user & customizer
Re: PropertySetDefinition with a List data type
« Reply #7 on: June 25, 2024, 07:19:11 PM »
Well, here it is a few days later and I have found that I wasn't able to do everything I needed using Christopher's code. It will come in handy for other tools, just not this one.

So Google and I spent more quality time together. I ended up back on StackOverflow.com reading a thread  that I had previously read and tried the solution posted. That solution I was not able to get to work, but today I realized that the OP had posted a link to a Demo project. That one is much better than the one I posted previously and allowed me to quickly test various other solutions I found. Between that demo and this thread I finally have a working project!

Attached is a zip with the Demo project from the above SO thread modified to work as I needed.

Atook

  • Swamp Rat
  • Posts: 1031
  • AKA Tim
Re: PropertySetDefinition with a List data type
« Reply #8 on: June 26, 2024, 11:53:45 AM »
Well done sleuthing down the solution Jeff!

Thanks for posting the demo project you arrived at.  :-)