Thursday, April 28, 2011

How do I bind UserControl property?

Let's say I have a simple UserControl with no codebehind:

<UserControl
xmlns= ....
x:Class="TrafficLight.LightControl">
<Grid x:Name="LayoutRoot">
<Grid x:Name="Light" >
  <Ellipse  x:Name="MyColor" Fill="{**I need bind this field to UserControl.Foreground**}" Stroke="#FF000000" />
  <Ellipse  x:Name="Shade" Fill="#E4555555" />
</Grid>
</Grid>

And use this UserControl on the Page:

<TrafficLight:LightControl x:Name="red" Foreground="#FFFEF100" **<= I need to bind MyColor.Fill with this property**/>

How I can implement this scenario?

From stackoverflow
  • Okay, I looked into various methods and without ElementName binding (something that Silverlight 3 introduces), which Microsoft didn't include for Silverlight, it's a tough one. Without some code behind, this is not an easy task to achieve.

    This blog post gives some detail on how you could achieve this, but be aware that you will need to write some code.

    FFire : Thanks. I've try to use it, but no effect.
    Jeff Yates : Hmm, that's a shame. I'll try and take a look later today, and see if we can't sort this one out for you. :)

0 comments:

Post a Comment