This code works in WPF but not in Silverlight.
此代码适用于WPF,但不适用于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?
Silverlight中是否有任何变通方法可以让我将滑块值绑定到元素高度? Silverlight有什么限制?
ANSWER:
Thanks Peter for solving this, for others: here is the solution with online demo and downloadable code.
感谢Peter为其他人解决这个问题:这里是在线演示和可下载代码的解决方案。
<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>
1 个解决方案
#1
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.
Silverlight 2目前不支持ElementName绑定。此MSDN文档文章非常简洁地介绍了Silverlight当前和当前未提供的WPF功能的细节。
In the interim you have to use some sort of intermediary property, here is a full example:
在此期间你必须使用某种中间属性,这是一个完整的例子:
Page.xaml:
<UserControl x:Class="SilverlightApplication1.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<StackPanel x:Name="LayoutRoot" Background="AliceBlue">
<Slider x:Name="Slider1" Minimum="10" SmallChange="10"
LargeChange="20" Maximum="100"
Value="{Binding HelperValue, Mode=TwoWay}" Cursor="Hand"/>
<Rectangle x:Name="Rectangle1" Fill="Red"
Height="{Binding HelperValue, Mode=OneWay}" Width="100" />
</StackPanel>
</UserControl>
Page.xaml.cs:
using System;
using System.ComponentModel;
using System.Windows.Controls;
namespace SilverlightApplication1
{
public partial class Page : UserControl
{
BindingConduit<double> sliderToRect = new BindingConduit<double>(50.0);
public Page()
{
InitializeComponent();
LayoutRoot.DataContext = sliderToRect;
}
}
public class BindingConduit<T> : INotifyPropertyChanged where T : struct
{
private T helperValue;
public T HelperValue
{
get { return this.helperValue; }
set
{
if ((this.helperValue.Equals(value) != true))
{
this.helperValue = value;
this.RaisePropertyChanged("HelperValue");
}
}
}
public BindingConduit(T defaultValue)
{
HelperValue = defaultValue;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if (propertyChanged != null)
{
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
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.
Slider的Value属性绑定到HelperValue并设置为TwoWay,Rectangle的Height绑定到同一属性但只需要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.
正在从名为sliderToRect的Page中创建的BindingConduit
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).
BindingConduit
#1
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.
Silverlight 2目前不支持ElementName绑定。此MSDN文档文章非常简洁地介绍了Silverlight当前和当前未提供的WPF功能的细节。
In the interim you have to use some sort of intermediary property, here is a full example:
在此期间你必须使用某种中间属性,这是一个完整的例子:
Page.xaml:
<UserControl x:Class="SilverlightApplication1.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<StackPanel x:Name="LayoutRoot" Background="AliceBlue">
<Slider x:Name="Slider1" Minimum="10" SmallChange="10"
LargeChange="20" Maximum="100"
Value="{Binding HelperValue, Mode=TwoWay}" Cursor="Hand"/>
<Rectangle x:Name="Rectangle1" Fill="Red"
Height="{Binding HelperValue, Mode=OneWay}" Width="100" />
</StackPanel>
</UserControl>
Page.xaml.cs:
using System;
using System.ComponentModel;
using System.Windows.Controls;
namespace SilverlightApplication1
{
public partial class Page : UserControl
{
BindingConduit<double> sliderToRect = new BindingConduit<double>(50.0);
public Page()
{
InitializeComponent();
LayoutRoot.DataContext = sliderToRect;
}
}
public class BindingConduit<T> : INotifyPropertyChanged where T : struct
{
private T helperValue;
public T HelperValue
{
get { return this.helperValue; }
set
{
if ((this.helperValue.Equals(value) != true))
{
this.helperValue = value;
this.RaisePropertyChanged("HelperValue");
}
}
}
public BindingConduit(T defaultValue)
{
HelperValue = defaultValue;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if (propertyChanged != null)
{
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
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.
Slider的Value属性绑定到HelperValue并设置为TwoWay,Rectangle的Height绑定到同一属性但只需要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.
正在从名为sliderToRect的Page中创建的BindingConduit
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).
BindingConduit