Thanks for the quick replies!
However, none of your suggestions will retain the full functionalities of the ComboBox. It does work fine and having the ObjectCollection exposed is better functionality than a single link to the .Add method, and is enough for what I need to do (but so was my original code). Imagine a DataGrid - you will want to keep a LOT of the features it has and you probably don't want to relink everything manually...
I thought of something overnight though, and I just tried it and it works perfectly. It also answers my "what if you need 2 ComboBox to retain full functionality" question.
Basically, i was very close when I exposed a public variable that would link to my cboBox:
Code:
Class StealthComboBox
Public myBox as ComboBox = cboBox
End Class
For some reason, the above code builds correctly but gives a "NullReferenceException" when trying to access the myBox reference.
By changing the variable to a readonly property (this is what i dreamed about last night...):
Code:
Class StealthComboBox
Public Readonly Property myBox() As ComboBox
Get
Return cboBox
End Get
End Property
End Class
...not only it still builds correctly, but accessing to ".myBox" exposes the whole ComboBox members. They retain full functionality and even though the ".myBox" reference is readonly (which is what i want, obviously), any property within the ComboBox retains their original read/write settings.
If I would create a UserControl with, let's say, 2 ComboBox: one with Country and the other one with State, the easiest way to let the code access both of them would be:
Code:
(XAML)
<UserControl Name="CountryStateSelector">
<Grid>
<ComboBox Name="_CountrySelector" />
<ComboBox Name="_StateSelector" />
</Grid>
</UserControl>
(Code-Behind)
Class CountryStateSelector
Public Readonly Property CountrySelector() as ComboBox
Get
Return _CountrySelector
End Get
End Property
Public Readonly Property StateSelector() as ComboBox
Get
Return _StateSelector
End Get
End Property
End Class
Then in my application:
Code:
(XAML)
<my:CountryStateSelector Name="LocationSelector" />
(Code-Behind)
Public Sub somesub()
LocationSelector.CountrySelector.Items.Add("USA")
LocationSelector.CountrySelector.Items.Add("Canada")
LocationSelector.StateSelector.Items.Add("Boston")
LocationSelector.StateSelector.Items.Add("Quebec")
End Sub
Now I can access the whole combobox features with very minimal code.
The only drawback of this approach is that the combo box still isn't reachable through XAML so I can't use databinding. But for what I need to do, this is sufficient. I guess I could easily map a bindable property to the objectcollection afterward if i need databinding.
yay!
Bookmarks