1
<Slider Name="slider1" Height="Auto" Margin="3" Minimum="1" Maximum="40" Value="2" TickPlacement="BottomRight" TickFrequency="1"/> <TextBlock Name="textblock1" TextWrapping="Wrap" Text="TextBlock" Margin="10" FontSize="{Binding ElementName=slider1,Path=Value}"/>
Slider
Minimum="1" 最长度;Maximum="40" 最大长度;
Value 当前值;
TickPlacement="BottomRight" ; 刻度值放置位置;
TickFrequency="1" 刻度值,最小刻度。比如尺子,最小刻度毫米;
TextBlock
FontSize="{Binding ElementName=slider1,Path=Value}"
字体大小绑定slider1,帮助值为slider1.Value;
后台代码设置绑定
private void Window_Loaded(object sender, RoutedEventArgs e) { Binding binding = new Binding(); binding.Source = this.slider1;//绑定的控件 binding.Path = new PropertyPath("Value");//绑定的属性 binding.Mode = BindingMode.TwoWay;//双向同步,设置后,修改TextBlock属性值,slider1也会相应改变; this.textblock1.SetBinding(TextBlock.FontSizeProperty, binding);//属性值对应绑定值 }
Text="{Binding ElementName=textBlock1,Path=FontSize,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
PropertyChanged 当目标内容改变时,立即更新绑定的源控件对应的值。
--