silverlight資料繫結

來源:互聯網
上載者:User

標籤: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

silverlight資料繫結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.