以下程序在运行进名为“txtbl”的TextBlock控件无法显示
Silverlight项目:DataBindingDemo 文件:ElementNameDemo.xaml
....xmlns:local="clr-namespace:DataBindingDemo"...
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <TextBlock Height="30" HorizontalAlignment="Left" Margin="118,54,0,0" Name="txtblk" VerticalAlignment="Top" Width="210"> <TextBlock.Text> <Binding ElementName="slider1" Path="Value"> <Binding.Converter> <local:TruncationConverter/> </Binding.Converter> </Binding> </TextBlock.Text> </TextBlock> <Rectangle Height="200" HorizontalAlignment="Left" Margin="333,224,0,0" Name="rectangle1" Stroke="Yellow" StrokeThickness="1" VerticalAlignment="Top"> <Rectangle.Width> <Binding ElementName="slider1" Path="Value"></Binding> </Rectangle.Width> <Rectangle.RenderTransform> <RotateTransform x:Name="rotate" Angle="90"/> </Rectangle.RenderTransform> </Rectangle> <Slider Height="256" HorizontalAlignment="Left" Margin="2,345,0,0" Name="slider1" VerticalAlignment="Top" Width="448" Maximum="360" /> </Grid>
隐藏文件:
public ElementNameDemo() {
InitializeComponent(); Binding binding = new Binding(); binding.ElementName = "rotate"; binding.Path = new PropertyPath("Angle"); binding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(slider1, Slider.ValueProperty, binding); }
TruncationConverter类:
public class TruncationConverter:IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is double) //将双精度浮点舍入为最接近的整数值 return Math.Round((double)value); return value; }
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return value; }
建议重新排一下版并加上描述问题的文字