界面元素之間須要聯動,可使用綁定ide
<StackPanel Orientation="Horizontal"> <TextBox x:Name="textBox" Width="50" Margin="10 " VerticalAlignment="Center"/> <Slider x:Name="mySlider" Minimum="10" Maximum="50" SmallChange="1" Margin="10" Width="300" VerticalAlignment="Center" IsSnapToTickEnabled="True" TickFrequency="5" Value="{Binding ElementName=textBox,Path=Text,Mode=TwoWay}"/> </StackPanel>
IsSnapToTickEnabled="True" 對齊到刻度this
TickFrequency="5" 刻度間距5spa
Value="{Binding ElementName=textBox,Path=Text,Mode=TwoWay}" Slider(目標)的Value屬性與textBox(源)的Text屬性綁定,相互影響。code
固然,也能夠在CS中經過代碼實現:對象
private void Window_Loaded(object sender, RoutedEventArgs e) { Binding bind = new Binding(); //建立對象 bind.Mode = BindingMode.TwoWay; //雙向 bind.Source = this.textBox; //源textBox bind.Path = new PropertyPath("Text"); //textBox的Text屬性 mySlider.SetBinding(Slider.ValueProperty, bind); //與mySlider的Value屬性進行鏈接 }
運行的結果動態影響界面時,使用代碼來實現。blog