Binding to another control in Silverlight
Is t开发者_C百科here a way to bind to the instance of another control? Something like this:
<Button x:Name="Foo" Content="Foo" />
<local:CustomControl OtherControl="{Binding Foo}" />
I've tried setting DataContext to "this" in the constructor of MainPage but it doesn't seem to work.
CustomControl is defined something like this:
class CustomControl
{
public FrameworkElement OtherControl { get; set; }
}
Not sure what you trying to do but in Silverlight 3 you can use element binding to bind to a property on a control.
<Button x:Name="Foo" Content="Foo" />
<local:CustomControl x:Name="control" Property="{Binding Path=Content, ElementName=Foo}" />
In code you could always analyze the binding and get the element from that?
control.GetBindingExpression(Property).ParentBinding.Source
It is impossible in Silverlight 2:
Silverlight 2 doesn’t allow you to bind one element to another element. Instead, all bindings are to data objects. (You could get around this using an intermediate object, but the added inconvenience means it’s rarely worthwhile.)
<Button x:Name="Foo" Content="Foo" />
<local:CustomControl x:Name="control" OtherControl="{Binding ElementName=Foo}" />
精彩评论