I've created a custom class (Autocad template class)
Imports System.Collections.ObjectModel
Imports Autodesk.AutoCAD.DatabaseServices
Namespace Model
Public Class TemplateFile
Public Property SelectedTemplateFileName() As String
Public Property SelectedTemplateFilePath() As String
Private _templateLayoutName As ObservableCollection(Of String)
Public Sub New()
End Sub
Public ReadOnly Property TemplateLayoutNameList() As ObservableCollection(Of String)
Get
Return ListLayoutName(SelectedTemplateFilePath)
End Get
End Property
''' <summary>
''' this function return a list of all layout name (as string) on an external drawing
''' </summary>
''' <param name="filename"></param>
''' <returns></returns>
Private Shared Function ListLayoutName(ByVal filename As String)
Dim layoutName As ObservableCollection(Of String) = New ObservableCollection(Of String)
' Create a new database object and open the drawing into memory
Dim acExDb As Database = New Database(False, True)
acExDb.ReadDwgFile(filename, FileOpenMode.OpenForReadAndAllShare, True, "")
' Create a transaction for the external drawing
Using acTransEx As Transaction = acExDb.TransactionManager.StartTransaction()
' Get the layouts dictionary
Dim layoutsEx As DBDictionary = acTransEx.GetObject(acExDb.LayoutDictionaryId, OpenMode.ForRead)
For Each entry As DBDictionaryEntry In layoutsEx
layoutName.Add(entry.Key)
Next
End Using
Return layoutName
End Function
End Class
End Namespace
& I would like to bind an instance of the class to a ComboBox (Xaml below). My path=TemplateLayoutNameList, source=_selectTemplate & target=TemplateLayoutNameComboBox
<ComboBox x:Name="TemplateLayoutNameComboBox"
HorizontalAlignment="Left"
ItemsSource="{Binding TemplateLayoutNameList, UpdateSourceTrigger=PropertyChanged}"
Margin="10,10,0,10"
Grid.Row="8"
Width="401" Grid.ColumnSpan="2"
Grid.Column="0" />
& here is my code behind
Public Class Window1
Private Shared _selectTemplate As TemplateFile
Private Sub Window_Loaded(sender As Object, e As Windows.RoutedEventArgs)
_selectTemplate = New TemplateFile()
End Sub
Private Sub button_Click_1(sender As Object, e As Windows.RoutedEventArgs) Handles button.Click
'browse for the files to be processed...
Dim openFileDialog As New OpenFileDialog()
openFileDialog.Multiselect = False
openFileDialog.Filter = "AutoCAD files (*.dwg)|*.dwg|Drawing Template (*.dwt)|*.dwt|All files (*.*)|*.*"
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
If openFileDialog.ShowDialog() = True Then
_selectTemplate.SelectedTemplateFileName = Path.GetFileName(openFileDialog.FileName)
_selectTemplate.SelectedTemplateFilePath = Path.GetFullPath(openFileDialog.FileName)
End If
'set binding for combobox template layout
Dim bin As New Binding("TemplateLayoutNameList")
bin.Mode = BindingMode.OneWay
bin.Source = _selectTemplate
TemplateLayoutNameComboBox.DataContext = _selectTemplate
BindingOperations.SetBinding(TemplateLayoutNameComboBox, ComboBox.TextProperty, bin)
End Sub
End Class
So when I excute this, the combobox get filled the first time (good) but it does not update for the later iteration. I know i'm doing this all the wrong way... Basically, I would like to bind the combobox to a read-only property (collection). Anyone could point me in the right direction...