מדריך Windows Phone

מדריך Windows Phone – שימוש ב Data Binding: כיוון זרימת המידע

‏ • Sela

כאמור ניתן לשלוט בכיוון זרימת המידע מאובייקט המקור לאובייקט היעד. כדי לעשות יש לשנות את התכונה Mode של אובייקט ה Binding. האפשרויות לתכונה זו הן:

  1. OneTime – הערך של תכונת המקור יועתק לתכונת היעד פעם אחת בלבד.
  2. OneWay – הערך של תכונת המקור יועתק לתכונת היעד בכל פעם שתכונת המקור תשתנה.
  3. TwoWay – סנכרון דו-כיווני בין תכונת המקור לתכונת היעד. כלומר, בכל פעם שתכונת המקור תשתנה, תכונת היעד תעודכן. וכן, בכל פעם שתכונת היעד תשתנה, תכונת המקור תעודכן.

דוגמת הקוד הבאה מדגימה את שלושת האפשרויות לתכונה Mode:

<phone:PhoneApplicationPage
  x:Class="PhoneDemo.MainPage"
  >="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  >:x="http://schemas.microsoft.com/winfx/2006/xaml"
  >:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
  >:d="http://schemas.microsoft.com/expression/blend/2008"
  >:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="d"
  d:DesignWidth="480"
  d:DesignHeight="800"
  FontFamily="{StaticResource PhoneFontFamilyNormal}"
  FontSize="{StaticResource PhoneFontSizeNormal}"
  Foreground="{StaticResource PhoneForegroundBrush}"
  Orientation="Portrait"
  SupportedOrientations="Portrait">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <TextBlock
     Grid.Row="0"
     Grid.Column="0"
     Text="Mode"
     FontWeight="Bold" />
    <TextBlock
     Grid.Row="0"
     Grid.Column="1"
     Text="Source"
     FontWeight="Bold" />
    <TextBlock
     Grid.Row="0"
     Grid.Column="2"
     Text="Target"
     FontWeight="Bold" />
    <TextBlock
     Grid.Row="1"
     Grid.Column="0"
     Text="OneTime" />
    <TextBlock
     Grid.Row="2"
     Grid.Column="0"
     Text="OneWay" />
    <TextBlock
     Grid.Row="3"
     Grid.Column="0"
     Text="TwoWay" />
    <CheckBox
     x:Name="source1"
     Grid.Row="1"
     Grid.Column="1"
     IsChecked="True" />
    <CheckBox
     x:Name="source2"
     Grid.Row="2"
     Grid.Column="1"
     IsChecked="True" />
    <CheckBox
     x:Name="source3"
     Grid.Row="3"
     Grid.Column="1"
     IsChecked="True" />
    <CheckBox
     x:Name="target1"
     Grid.Row="1"
 
    
Grid.Column="2"
     IsChecked="{Binding ElementName=source1, Path=IsChecked, Mode=OneTime}" />
    <CheckBox
     x:Name="target2"
     Grid.Row="2"
     Grid.Column="2"
     IsChecked="{Binding ElementName=source2, Path=IsChecked, Mode=OneWay}" />
    <CheckBox
     x:Name="target3"
     Grid.Row="3"
     Grid.Column="2"
     IsChecked="{Binding ElementName=source3, Path=IsChecked, Mode=TwoWay}" />
  </Grid
>
</
phone:PhoneApplicationPage
>

מומלץ להעתיק תוכנית זו ולהתנסות בעצמכם איך כל אפשרות Mode של Binding מתנהגת.

מסך הפתיחה של תוכנית זו נראה כך:

מדריך Windows Phone – שימוש ב Data Binding: כיוון זרימת המידע

תגיות: , , , ,

arikp

אריק פוזננסקי הוא יועץ בכיר ומרצה בסלע. הוא השלים שני תארי B.Sc. במתמטיקה ומדעי המחשב בהצטיינות יתרה בטכניון. לאריק ידע נרחב בטכנולוגיות מיקרוסופט, כולל .NET עם C#, WPF, Silverlight, WinForms, Interop, COM/ATL, C++ Win32 ו reverse engineering.

תגובות בפייסבוק