123wys 发表于 2015-12-22 21:54:27

silverlight中数据绑定

silverlight中数据绑定 控件绑定    <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.Children.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-co**tibility/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-co**tibility/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”.

tianshanxue 发表于 2015-12-22 21:54:27

{:1106_362:}
谢谢楼主分享

zhuyilong32 发表于 2015-12-22 21:54:27

好专业。。。。。。。。。的样子
页: [1]
查看完整版本: silverlight中数据绑定