I come from VBA and I am programming above my capacities, but without frustration. I'm stuck, so I seek help.
I have this WPF form that has a combobox populated with the layer names. In my design the selection of a layer is optional, for that reason item[0] is a message "<Do not filter on layer>". Then from item 1 onwards the combobox is populated with the layernames.
After some thinking I made a peculiar choice and started implementing a Dictionary<string, SmallLayerObject> variable. That seems complex to me, but worth the try, for it seemed strong and flexible. I read from the LayerTable into a SortedList<string, SmallLayerObject>. That code looks like this:
Transaction trans = DrawingDatabase.TransactionManager.StartTransaction();
SortedList
<string, SmallLayerObj
> LayersSorted
= new SortedList
<string, SmallLayerObj
>();
LayerTable lyrTbl = trans.GetObject(DrawingDatabase.LayerTableId, OpenMode.ForRead) as LayerTable;
foreach (ObjectId lyrId in lyrTbl)
{
lyrTblRec = trans.GetObject(lyrId, OpenMode.ForRead) as LayerTableRecord;
lyrObj
= new SmallLayerObj
{
BaseId = lyrId,
IsFrozen = lyrTblRec.IsFrozen,
Name = lyrTblRec.Name
};
LayersSorted.Add(lyrTblRec.Name, lyrObj);
}
Dictionary
<string, SmallLayerObj
> LayerDict
= new Dictionary
<string, SmallLayerObj
> {
// the defMessage is a string "<Do not filter on layer>"
{ defMessage,
new SmallLayerObj
{ Name
= null, BaseId
= ObjectId
.Null, IsFrozen
= false } } };
foreach (KeyValuePair<string, SmallLayerObj> layerName in LayersSorted)
{
LayerDict.Add(layerName.Key, layerName.Value);
}
return LayerDict;
It still needs attention, but it works. In the long run it will allow me to expand the SmallLayerObj without much effort.
Now my data is bound with this XAML syntax:
ItemsSource="{Binding}" DisplayMemberPath="Key" SelectedValuePath="Value.BaseId". I am not sure if the SelectedValuePath="Value.BaseId" is working and what it is supposed to do, but the DisplayMemberPath="Key" is working and the combobox is populated with the layernames.
Now I have this return value
var lyr = win.cboLayers.SelectedItem;
and the lyr variable is actually a Generic.KeyValuePair
My problem is that I have no clue how to read out the ObjectId that is stored in the Value part that is a SmallLayerObj with three items. .TryGetValue? using an indexer? I have no clue and even the IntelliSense does not offer help. It is beyond my reach...