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进行简化的绑定:

  • 相关阅读:
    写在彻底转向有道云笔记一个月之后
    KMP算法实现
    有道云笔记 V.S. 为知笔记
    卸载印象笔记,跟印象笔记说拜拜
    ExpandRegion for Sublime Text:快速选择文本
    Linux cat命令详解
    Vim安装插件
    Vim与正则表达式
    还没供暖
    在Linux命令行中设置并使用代理服务器
  • 原文地址:https://www.cnblogs.com/frogkiller/p/12917441.html
Copyright © 2011-2022 走看看