Posts Tagged DataBinding
DataBinding ObservableCollection with WPF
Watch out for the code you place in your sub New() of the ObservableCollection – it can cause the WPF designer problems as the designer will attempt to execute the constructor during design time. If you’re trying to populate the collection from a file based database then the connection string, in some situations, VS 2008 will look in the wrong place for the database. It appears that using the |Data Directory| prefix for the database causes VS 2008 (RTM) to look in the working directory of VS – rather than the project directory.
Symptoms of this kind of problem are VS reporting:
“Could not create an instance of type ‘abc’. …Window1.xaml 8 3″ and your XAML reference will be squiggily underlined.
A solution to this problem is wraping the constructor of the object implementing the ObservableCollection in a try/catch and swallowing the error (suggested only during the development cycle) or by detecting if you’re in designtime.
A simple way to detect if you’re in designtime with WPF is:
If DesignerProperties.GetIsInDesignMode(Application.Current.MainWindow) Then
Add(New contact("DesignTime", "Detected"))
Else
Try
oContactsTA.Fill(oContacts)
For Each thisRow In oContacts
Add(New contact(thisRow))
Next
Catch ex As Exception
Add(New contact("DesignTime", "NotDetected"))
End Try
End If
3 comments Saturday 24th November, 2007