How to create databinding over two xaml files?

Posted by BionicGecko on Stack Overflow See other posts from Stack Overflow or by BionicGecko
Published on 2010-05-14T15:29:37Z Indexed on 2010/05/14 15:34 UTC
Read the original article Hit count: 322

Hello,

I am trying to come to a working understanding of how databinding works, but even after several tutorials I only have a basic understanding of how databinding works. Thus this question might seem fundamental to those more familiar with silverlight. Even if it is trivial, please point me to some tutorial that deals with this problem. All that I could find simply solved this via adding the data binding on a parent page.xaml (that i must not use in my case).

For the sake of this example let us assume, that we have 5 files:

starter.cs
button1.xaml + codeBehind
button2.xaml + codeBehind

The two buttons are generated in code in the starter(.cs) file, and then added to some MapLayer

button1 my_button1 = new button1();   
button2 my_button1 = new button2();

someLayer.Children.Add(my_button1);
someLayer.Children.Add(my_button2);     

My aim is to connect the two buttons, so that they always display the same "text" (i.e. my_button1.content==my_button2.content = true;). Thus when something changes my_button1.content this change should be propagated to the other button (two way binding).

At the moment my button1.xaml looks like this:

<Grid x:Name="LayoutRoot">
<Button x:Name="x_button1" Margin="0,0,0,0" Content="{Binding ElementName=x_button2, Path=Content}" ClickMode="Press" Click="button1_Click"/>
</Grid>

But everthing that i get out of that is a button with no content at all, it is just blank as the binding silently fails. How could I create the databinding in the context I described? Preferably in code and not XAML ;)

Thanks in advance

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about silverlight-3.0