zoukankan      html  css  js  c++  java
  • wpf---数据绑定:

    1,数据绑定的几种方式:

    image

    image

    重点解释以下几点:1,目标对象的属性是依赖项属性.

                            2,对于Default方式,当目标属性可以设置时,则是双向绑定,否则是单向绑定.

    2,使用代码绑定和解除绑定:

                Binding binding = new Binding();
                binding.Source = silderFontSize;//绑定数据源
                binding.Path = new PropertyPath("Value");//注意 使用新类 PropertyPath
                binding.Mode = BindingMode.TwoWay;
                txtBlock.SetBinding(TextBox.TextProperty, binding);//注意 不是 txtBlock.Text---这是string类型,而是TextBlock.TextProperty

    获取绑定:

    Binding binding = BindingOperations.GetBinding(obj,dependencyProperty)//获取绑定
    BindingExpression expression = BindingOperations.GetBindingExpression(obj,dependencyProperty);
    object SourceObj = expression.ResolvedSource;
    //操作源对象.

    3,对于TextBox,虽然是双向绑定,但是只有在失去焦点时候才更新值,所以,可以设定 更新源的方式:

    反向更新并不会立刻发生: 这取决于目标属性的方式:

    image

      Delay: 设定延迟触发源的时间.


    利用expression.UpdateSource进行源更新.

      Binding binding = new Binding();
                binding.Source = this;
                binding.Path = new PropertyPath("ContentBox");
                binding.Mode = BindingMode.TwoWay;
                binding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
                inputBox.SetBinding(TextBox.TextProperty, binding);

    利用XAML的RelativeSource方法进行测试

     <TextBox x:Name="inputBox" Height="28" Canvas.Left="28" TextWrapping="Wrap"  Canvas.Top="21" Width="169" TextAlignment="Center" FontSize="20"
                     Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}},Path=ContentBox,Mode=TwoWay,UpdateSourceTrigger=Explicit}">
    
    
    
           

    image


    利用 Datacontext进行简化的绑定:

  • 相关阅读:
    简单的JS控制button颜色随点击更改
    以 “月” 为单位的可以翻页的效果 显示为 2016年01月
    荷兰国旗 Flag of the Kingdom of the Netherlands
    最大连续子数组以及拓展
    Something Wrong or Something Right
    转载文章----十步完全理解SQL
    数据库关系代数练习题
    SQL server 2014安装以及解决连接数据库失败问题
    回文判断
    字符串的包含
  • 原文地址:https://www.cnblogs.com/frogkiller/p/12917441.html
Copyright © 2011-2022 走看看