WPF学习笔记<二>数据绑定

  • 1 bind a property of an element to itself
 1:  <Window x:Class="WpfApplication1.chap5_2"
 2:          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3:          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4:          Title="chap5_2" Height="300" Width="300">
 5:      <Grid>
 6:          <Slider Name="slider" 
 7:                      Margin="4" Interval="1" 
 8:                      TickFrequency="1" 
 9:                      IsSnapToTickEnabled="True"
10:                      Minimum="0" Maximum="100"
11:                      ToolTip="{Binding
12:                                   RelativeSource =
13:                                  { RelativeSource Self},
14:                                 Path=Value}"/>
15:      </Grid>
16:  </Window>
17:  
其中Binding RelativeSource={RelativeSource Self}等价于Binding RelativeSource=
{x:Static RelativeSource.Self}
2010-11-09_140731 

2 create a two-way binding

2010-11-09_142338

 1:  <Window x:Class="WpfApplication1.chap5_2"
 2:          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3:          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4:          Title="chap5_2" Height="300" Width="300">
 5:      <Grid>
 6:          <Slider Name="slider" 
 7:                      Margin="4" Interval="1" 
 8:                      TickFrequency="1" 
 9:                      IsSnapToTickEnabled="True"
10:                      Minimum="0" Maximum="100"/>
11:              <!--    ToolTip="{Binding
12:                                   RelativeSource =
13:                                  { x:Static    RelativeSource.Self},
14:                                 Path=Value}"/>-->
15:        <StackPanel Orientation="Horizontal">
16:                  <TextBlock 
17:                      Width="Auto" HorizontalAlignment="Left"
18:                      Margin="4" VerticalAlignment="Center" 
19:                      Text="Gets and sets the value of the slider:" />
20:                  <TextBox Width="31" HorizontalAlignment="Center" Margin="4"
21:                             Text="{Binding
22:                                          ElementName=slider,
23:                                          Path=Value, 
24:                                          Mode=TwoWay, 
25:                                          UpdateSourceTrigger=PropertyChanged}" Height="25" />
26:              </StackPanel>
27:      </Grid>
28:  </Window>
29:  

本例中,设置TextBox的 mode=TwoWay,可以实现在文本框中显示滑动条上的数据,也可修改文本框中数据使滑动条滑动到数据对应的指定位置。

UpdateSourceTrigger指定事件触发更新的发生时间。

2010-11-09_142558

原文地址:https://www.cnblogs.com/gisalameda/p/1872898.html