Joseph Liberty
Joseph Liberty

Reputation:

Binding to another control in Silverlight

Is there 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; }
}

Upvotes: 4

Views: 4200

Answers (3)

Jaider
Jaider

Reputation: 14874

<Button x:Name="Foo" Content="Foo" />
<local:CustomControl x:Name="control" OtherControl="{Binding ElementName=Foo}" />

Upvotes: 1

afriend
afriend

Reputation: 71

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

Upvotes: 7

Dmytro Laptin
Dmytro Laptin

Reputation: 611

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.)

Upvotes: 1

Related Questions