留在屏幕上的字

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

路径就是用来设置binding要关联的那个属性

XMAL代码

<StackPanel Background="LightBlue">
<!--案例一-->
<!--写法一-->
<!--<TextBox Margin="5" Height="20" Text="{Binding Path=Value,ElementName=slider}"/>-->
<!--写法二-->
<!--<TextBox x:Name="txtbox1" Margin="5" Height="20" Text="{Binding Path=Value,ElementName=slider}"/>
<Slider x:Name="slider" Minimum="0" Maximum="100" Margin="5"/>-->

<!--案例二-->
<!--<TextBox x:Name="txtbox1" Margin="5"/>
<TextBox x:Name="txtbox2" Margin="5" Text="{Binding Path=Text.Length,ElementName=txtbox1,Mode=OneWay}"/>-->

<!--案例三-->
<TextBox x:Name="txtbox1" Margin="5"/>
<TextBox x:Name="txtbox2" Margin="5" Text="{Binding Path=Text.[3],ElementName=txtbox1,Mode=OneWay}"/>
</StackPanel>

CS代码

public MainWindow()
{
InitializeComponent();
//方案一
//Binding binding = new Binding() {Path = new PropertyPath("Value"), Source = slider};
//写法三
//Binding binding = new Binding("Value") {Source = slider};
//this.txtbox1.SetBinding(TextBox.TextProperty, binding);

}

当使用一个集合或者DataView作为Binding源的时候,我们可以把他的默认元素当作Path来使用

XMAL代码

<TextBox x:Name="txtOne" Margin="5"/>
<TextBox x:Name="txtTwo" Margin="5"/>
<TextBox x:Name="txtThree" Margin="5"/>

CS代码

List<string> list = new List<string>() {"aaa", "bb", "c"};
this.txtOne.SetBinding(TextBox.TextProperty, new Binding("/") {Source = list});
this.txtTwo.SetBinding(TextBox.TextProperty, new Binding("/Length") { Source = list ,Mode = BindingMode.OneWay});
this.txtThree.SetBinding(TextBox.TextProperty, new Binding("/[1]") { Source = list, Mode = BindingMode.OneWay });

 没有Pa他还的Binding

XMAL代码

<Window x:Class="没有Path的Binding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<StackPanel Background="LightBlue">
<StackPanel.Resources>
<sys:String x:Key="s">
dfdsfdsfdsfsdfsdfsdfsdfsdfsdfsd
</sys:String>
</StackPanel.Resources>
<TextBlock Text="{Binding Path=.,Source={StaticResource ResourceKey=s}}"/>
</StackPanel>
</Window>

 

posted on 2013-06-16 14:39  简单点,再简单点  阅读(2004)  评论(0编辑  收藏  举报
                                                                              多交流,多思考