网站淘宝客怎么做的,公司部门英文缩写简称大全,thinkphp 企业网站源码,房产网南京一.Binding的源与路径 在大多数情况下Binding的源是逻辑层的对象#xff0c;但有时候为了让UI元素产生一些联动效果也会使用Binding在控件间建立关联#xff0c; 下面的代码是把一个TextBox的Text属性关联在了Slider的Value上 Window x:ClassMyBinding.WindowBindi… 一.Binding的源与路径 在大多数情况下Binding的源是逻辑层的对象但有时候为了让UI元素产生一些联动效果也会使用Binding在控件间建立关联 下面的代码是把一个TextBox的Text属性关联在了Slider的Value上 Window x:ClassMyBinding.WindowBindingxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlTitleWindowBinding Height150 Width300GridStackPanelTextBox x:NametextBox1 Text{Binding PathValue,ElementNameSlider1} BorderBrushBlack Margin5 Height30 VerticalContentAlignmentCenter/Slider x:NameSlider1 Maximum100 Minimum0 Margin5//StackPanel/Grid
/Window 与之等价的C#代码是 Binding binding new Binding(Value) { Source Slider1 };this.textBox1.SetBinding(TextBox.TextProperty, binding); 二.控制Binding的方向及数据更新 Binding在源与目标之间架起了沟通的桥梁默认情况下数据既能通过Binding送达目标也能够从目标返回源(收集用户对数据的修改)。有时候数据只需要展示给用户 、不允许用户修改这时候可以把Binding模式更改为从源向目标的单向沟通Binding还支持从目标向源的单向沟通以及只在Binding关系确立时读取一次数据。 控制Binidng数据流向的属性是Mode它的类型是BindingMode枚举。BindingMode可取值为TwoWay、OneWay、OnTime、OneWayToSource和Default。这里 的Default值是指Binding的模式会根据目标的实际情况来确定比如若是可编辑的(TextBox.Text属性)Default就采用双向模式、若是只读的(TextBlock.Text)则采用单向模式 当我们拖动Slider的手柄时TextBox里就会显示出Slider当前的值如果我们在TextBox里输入一个恰当的值然后按一下Tab键让焦点离开TextBox则Slider的手柄会跳到 相应的值那里为什么一定要在TextBox失去焦点之后Slider的值才会改变这就引出了Binding的另一个属性——UpdateSourceTrigger它的类型是UpdateSourceTrigger枚举 可取值为PropertyChanged、LostFocus、Explicit和Default。对于TextBox默认值Default的行为与LostFocus一致我们只要把这个属性改为PropertyChanged则Slider的手柄 就会随着我们在TextBox里输入而改变位置 三.使用ObjectDataProvider对象作为Binding的Source 由于一个类的所有数据都使用属性暴露出来比如我们需要的数据可能是方法的返回值这时候就需要使用ObjectDataProvider来包装作为Binding源的数据对象了 如下 public class Calculator{//加法public string Add(string arg1, string arg2){double x 0;double y 0;double z 0;if(double.TryParse(arg1, out x) double.TryParse(arg2,out y)){z x y;return z.ToString();}return Input Error!;}} Window x:ClassMyBinding.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlTitleMainWindow Height180 Width300 WindowStartupLocationCenterScreenGridStackPanel BackgroundLightBlueTextBox x:NametextBoxArg1 Margin5 Height30/TextBox x:NametextBoxArg2 Margin5 Height30/TextBox x:NametextBoxResult Margin5 Height30//StackPanel/Grid
/Window 这个程序实现的功能是上面两个TextBox输入数字后第3个TextBox能实时地显示数字的和 private void SetBinding(){//创建并配置ObjectDataProvider对象ObjectDataProvider odp new ObjectDataProvider();odp.ObjectInstance new Calculator();odp.MethodName Add;odp.MethodParameters.Add(0);odp.MethodParameters.Add(0);//以ObjectDataProvider对象为Source创建BindingBinding bindingToArg1 new Binding(MethodParameters[0]){Source odp,BindsDirectlyToSource true,UpdateSourceTrigger UpdateSourceTrigger.PropertyChanged};Binding bindingToArg2 new Binding(MethodParameters[1]){Source odp,BindsDirectlyToSource true,UpdateSourceTrigger UpdateSourceTrigger.PropertyChanged};Binding bindingToResult new Binding(.) { Source odp };//将Binding关联到UI元素上this.textBoxArg1.SetBinding(TextBox.TextProperty, bindingToArg1);this.textBoxArg2.SetBinding(TextBox.TextProperty, bindingToArg2);this.textBoxResult.SetBinding(TextBox.TextProperty, bindingToResult);} 四.使用Binding的RelativeSource 当一个Binding有明确的数据来源时我们可以通过为Source或ElementName赋值的办法让Binding与之关联有时候我们不能确定作为Souce的对象叫什么但知道它作为Binding目标的对象在UI布局上有 相对关系比如控件自己关联自己的某个数据、关联自己某级容器的数据这时候我们就要使用Binding的RelativeSource属性 Window x:ClassMyBinding.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlTitleMainWindow Height180 Width300 WindowStartupLocationCenterScreenGrid x:Nameg1 BackgroundRed Margin10DockPanel x:Named1 BackgroundOrange Margin10Grid x:Nameg2 BackgroundYellow Margin10DockPanel x:Named2 BackgroundLawnGreen Margin10TextBox x:NametextBox1 FontSize24 Margin10 Text{Binding RelativeSource{RelativeSource FindAncestor,AncestorType{x:Type Grid},AncestorLevel1},PathName}//DockPanel/Grid/DockPanel/Grid
/Window AncestorLevel属性指的是Binding目标控件作为起点的层级偏移量——d2的偏移量是1、g2的偏移量是2以此类推AncestorType属性告诉Binding寻找哪个类型的对象作为自己的源 不是这个类型的对象会被跳过上面这段代码的意思是告诉Binding从自己的第一层依次向外找找到第一个Grid类型对象后把它当做自己的源。 转载于:https://www.cnblogs.com/QingYiShouJiuRen/p/10497388.html