I have a UserControl which contains a ComboBox. How do I expose the SelectedItem property of the ComboBox such that when the UserControl is used in XAML I can use the exposed property for binding? e.g.,
<myControls:MyControl Grid.Row="9" Grid.Column="0" ??SelectedItemExposed?? = {Binding ...etc ... Mode=TwoWay ... etc ... }" />
Would the 'SelectedItemExposed' be a Dependency Property? If so does it just reference the ComboBox.SelectedItem property somehow, and when the SelectionChanged event fires on the ComboBox within the UserControl, do I need to do anything in an event handler which updates the DependencyProperty? Thanks in advance for any assistance, examples, etc.
Answer: 1
You're right, you must add a depency property in your UC code.
Call it SelectedItem (or anything else). in your code you must add a private event handler for the property changed of the "true" SelectedItem, this event handled will take the current value of the combobox and set the "SelectedItem" you declared in your control.
your DP must hold the value changed, and this time when there is a value changed of your property, you set the combobox SelectedItem.
So your DP is just a proxy for the real underlying combobox property. Any change to your DP is made to the combobox one, any change to the combobox is propagate to your DP.
If you're not planing to make any binding on your property you can declare it just as a normal property, no need for a DP. But, of course, it is far a better way to create a DP.
by :Answer: 2
Got it working, thanks!
by :
No comments:
Post a Comment
Send us your comment related to the topic mentioned on the blog