首页 > 其他 > 详细

【转】WPF绑定模式

时间:2014-02-20 02:19:48      阅读:364      评论:0      收藏:0      [点我收藏+]

源地址:http://www.cnblogs.com/zjz008/archive/2010/05/26/1744802.html

    http://blog.csdn.net/haylhf/article/details/7628979

 

WPF支持OneWay,TwoWay,OneTime,Default和OneWayToSource等多种绑定模式

  • 无论是目标属性还是源属性,只要发生了更改,TwoWay 就会更新目标属性或源属性。

  • OneWay 仅当源属性发生更改时更新目标属性。

  • OneTime 仅当应用程序启动时或 DataContext 进行更改时更新目标属性。

  • OneWayToSource 在目标属性更改时更新源属性。

  • Default:使用目标属性的默认 Mode 值。

    下面的例子,展示了这几种绑定模式的区别:

     

    bubuko.com,布布扣
    bubuko.com,布布扣
    bubuko.com,布布扣代码
    <Window x:Class="WPF_Started.Controls.DataBinding.BindingMode"
            xmlns
    ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x
    ="http://schemas.microsoft.com/winfx/2006/xaml"
            Title
    ="BindingMode" Height="400" Width="300">
        
    <StackPanel>
            
    <ScrollBar Name="scroll" Orientation="Horizontal" Margin="20" Maximum="100" LargeChange="10" SmallChange="1" />
            
    <Label Content="OneWay:" Height="30" />
            
    <TextBox Name="scrollValue" Height="20" Width="200" HorizontalAlignment="Center" 
                     Text
    ="{Binding ElementName=scroll, Path=Value, Mode=OneWay}" />

            
    <Label Content="TwoWay:" Height="30" />
            
    <TextBox Name="scrollValueTwoWay" Height="20" Width="200" HorizontalAlignment="Center" 
                     Text
    ="{Binding ElementName=scroll, Path=Value, Mode=TwoWay}" />

            
    <Label Content="OneTime:" Height="30" />
            
    <TextBox Name="scrollValueOneTime" Height="20" Width="200" HorizontalAlignment="Center" 
                     Text
    ="{Binding ElementName=scroll, Path=Value, Mode=OneTime}" />

            
    <Label Content="OneWayToSource:" Height="30" />
            
    <TextBox Name="scrollValueOneWayToSource" Height="20" Width="200" HorizontalAlignment="Center" 
                     Text
    ="{Binding ElementName=scroll, Path=Value, Mode=OneWayToSource}" />

            
    <Label Content="Default:" Height="30" />
            
    <TextBox Name="scrollValueDefault" Height="20" Width="200" HorizontalAlignment="Center" 
                     Text
    ="{Binding ElementName=scroll, Path=Value, Mode=Default}" />
        
    </StackPanel>
    </Window>
    bubuko.com,布布扣
    bubuko.com,布布扣

     

     

     

    bubuko.com,布布扣

     

    运行结果显示,OneWay,TwoWay,Default模式下TextBox中的值随着滑块的位置而变化。OneTime模式下总是滑块的初始值0,这种模式下TextBox的值取决于ScrollBar的初始值。

    OneWayToSource模式下,在TextBox中输入20,滑动条滑块会自动滑倒20的位置,但当滑动条继续滑动时候,该模式下TextBox的值并不随着滑动条的滑动而改变。

【转】WPF绑定模式

原文:http://www.cnblogs.com/summer_adai/p/3556213.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!