binding checkbox in datatemplate to user control datacontext
I have a listbox which include a data template of 开发者_运维百科checkbox. I need the check box data context be the user control and not the listbox datacontext. How can active that in silverlight since donot have relativesource than self or template
Thanks in advance
Silverlight does not have relative source binding "out of the box", however there is a solution here that works:
http://www.scottlogic.co.uk/blog/colin/2009/02/relativesource-binding-in-silverlight/
You can bind your CheckBox's DataContext to that of the parent User Conntrol (In this example it is called MyUserControl) as follows:
<CheckBox>
<local:BindingHelper.Binding>
<local:BindingProperties TargetProperty="DataContext"
SourceProperty="DataContext"
RelativeSourceAncestorType="MyUserCOntrol"/>
</local:BindingHelper.Binding>
</CheckBox>
If you do not want to go down this route, you could create a ViewModel that exposes a collection of objects that you bind to your checkbox, each object could provide a reference back to the parent view model.
精彩评论