views:

1754

answers:

3

I am trying to bind a property of an element within a UserControl to a property set on the UserControl itself in Silverlight. I'm sure it should be simple, but I haven't managed to get it working with either RelativeSource or ElementName binding. In this example I want the Rectangle to be Green (or whatever the UserControl's Background property gets set to).

<UserControl x:Class="MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="40" Height="40" Background="Green" x:Name="root">
    <Grid x:Name="LayoutRoot" Background="White">
        <Rectangle x:Name="indicatorRectangle" Fill="{Binding Path=Background, ElementName=root}" Margin="0,0,26,0"  />
    </Grid>
</UserControl>

anyone know the correct binding syntax?

+2  A: 

Try this:

<UserControl ... Background="Green" x:Name="root">    
  <Grid x:Name="LayoutRoot" Background="White">        
    <Rectangle x:Name="indicatorRectangle" 
         Fill="{Binding Background, ElementName=root}" Width="10" Height="10"  />
  </Grid>
</UserControl>

It didn't work for me until I gave the rectangle a width and height.

Bryant
A: 

interestingly, the XAML I posted in the original question works correctly in VS2010, so I assume that this is something that has been fixed in the latest Silverlight

Mark Heath