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

  • 相关阅读:
    *VC编程规范
    C++的va_start() va_end()函数应用(转)
    * C++类的分解,抽象类与纯虚函数的需要性
    *C++中的回调
    *C++中使用接口
    C++模版使用
    *获取mac地址的方法
    *数字——字符之间的转换(转)
    eclipse雕虫小技一:eclipse打开文件目录
    Hibernate升级后注解方式的对象关系映射
  • 原文地址:https://www.cnblogs.com/frogkiller/p/12917441.html
Copyright © 2020-2023  润新知