This code works in WPF but not in Silverlight.
Are there any workarounds in Silverlight to enable to me to bind a slider value to element heights? What are the limitations of Silverlight here?
ANSWER:
Thanks Peter for solving this, for others: here is the solution with online demo and downloadable code.
<UserControl x:Class='Second12.Page' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:basics='clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls' Width='300' Height='200'> <Grid Background='Tan'> <StackPanel> <Canvas> <Border Background='#2200ffff' Canvas.Left='40' Canvas.Top='30' CornerRadius='5' BorderBrush='Brown' BorderThickness='1'> <Rectangle Height='{Binding ElementName=theSlider, Path=Value}' Width='50'/> </Border> </Canvas> </StackPanel> <Slider Name='theSlider' HorizontalAlignment='Left' Width='200' Cursor='Hand'/> </Grid> </UserControl>
ElementName binding is not currently supported in Silverlight 2. This MSDN documentation article is quite succinct on the specifics of the WPF functionality that Silverlight does and doesn’t currently provide.
In the interim you have to use some sort of intermediary property, here is a full example:
Page.xaml:
Page.xaml.cs:
The Value property of the Slider is bound to HelperValue and set to TwoWay, the Rectangle’s Height is bound to the same property but only requires OneWay.
The HelperValue property is being read from the instance of the BindingConduit<T> class created in Page called sliderToRect. This instance is set as the DataContext of the parent StackPanel control. Any change in Slider1’s Value property is reflected in the Height of Rectangle1 because the HelperValue property raises the PropertyChanged event.
BindingConduit<T> is a custom class I’ve created that implements INotifyPropertyChanged and also lets you specify a default value for HelperValue (i.e. the inital Value of Slider1 and the Height of Rectangle1 (50.0) in this case).