标签:des blog http io os 使用 ar strong for
<Grid x:Name="LayoutRoot">
<StackPanel>
<ScrollBar x:Name="bar" Orientation="Horizontal" Height="20" />
<TextBox x:Name="tb" Text="{Binding ElementName=bar, Path=Value, Mode=OneWay,
UpdateSourceTrigger=Default}" />
</StackPanel>
</Grid>
这段XAML代码定义了一个ScrollBar和一个TextBox,TextBox的Text会随着ScrollBar的拖动改变,范围从0到1.
Mode
为BindingMode枚举,有三个值,分别为OneTime,OneWay,TwoWay,分别是单次绑定,单向绑定和双向绑定。将上面代码的Mode改为TwoWay后,对TextBox值的更改同样也会影响ScrollBar的位置。
UpdateSourceTrigger
更改TextBox的值后,ScrollBar只有在TextBox失去焦点后才会做出改变。要改变这个行为,就需要更改UpdateSourceTrigger属性。同样有三个值:Default,PropertyChanged,Explicit。PropertyChanged会使ScrollBar实时反映TextBox的更改。而将属性改为Explicit后,就只能手动做出更新,方法如下:
tb.GetBindingExpression(TextBox.TextProperty).UpdateSource();
Path
绑定的Path可以不只是一个属性,比如
<TextBox x:Name="tb1" Text="{Binding ElementName=LayoutRoot, Path=Children[0].Children[0].Value}" />
但这是个傻例子 -_- 另外此时VS不会提供代码提示,出错的机会较大。
TypeConverter
实现一个Converter,将double转换为string并保留两位小数
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ((double)value).ToString(".00");
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return double.Parse(value.ToString());
}
}
<navigation:Page x:Class="SilverlightApp.DataBinding"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:local="clr-namespace:SilverlightApp"
d:DesignWidth="640" d:DesignHeight="480"
Title="DataBinding Page">
<navigation:Page.Resources>
<local:MyConverter x:Key="conv"/>
</navigation:Page.Resources>
<Grid x:Name="LayoutRoot">
<StackPanel>
<ScrollBar x:Name="bar" Orientation="Horizontal" Height="20" />
<TextBox x:Name="tb" Text="{Binding ElementName=bar, Path=Value, Mode=OneWay, Converter={StaticResource conv}, UpdateSourceTrigger=Default}" />
</StackPanel>
</Grid>
</navigation:Page>
Source/DataContext
如果数据源不是控件,应该使用Source属性。或者使用DataContext,此属性会在element tree中继承。一个Grid如果拥有DataContext,那么Grid中的所有element都可以绑定到此DataContext上。如下:
public class Contry
{
public string Name { get { return "China"; } }
}
XAML
<navigation:Page x:Class="SilverlightApp.DataBinding"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:local="clr-namespace:SilverlightApp"
d:DesignWidth="640" d:DesignHeight="480"
Title="DataBinding Page">
<navigation:Page.Resources>
<local:Contries x:Key="contry"/>
</navigation:Page.Resources>
<Grid x:Name="LayoutRoot">
<StackPanel>
<TextBox DataContext="{StaticResource contry}" Text="{Binding Name, Mode=OneWay}" />
<TextBox Text="{Binding Name, Source={StaticResource contry}}" />
</StackPanel>
</Grid>
</navigation:Page>
两个TextBox都可以正确的显示出“China”.
绑定目标必须是dependency property, 所以textbox.text可以,run.text不行,C#代码中,setbinding中第一个参数就是是dp
标签:des blog http io os 使用 ar strong for
原文地址:http://www.cnblogs.com/samulescollection/p/3974296.html