מדריך WPF – שימוש ב Data Binding: מועד ביצוע הסנכרון
אפשרות נוספת שניתנת לשליטה היא המועד שבו מתרחש סנכרון המידע. אפשרות הגיונית אחת היא לבצע סנכרון ברגע שהערך משתנה. אולם יש מקרים שבהם שיטה זו תהיה בזבזנית, חשבו על מצב שבו יש התכונה Text של TextBox מקושרת לתכונה מסוג String באובייקט נתונים, אם נבצע סנכרון בכל פעם שהמשתמש מקיש אות נוספת תתבצענה המון פעולות מיותרות. במקרה זה היה עדיף לעשות את הסנכרון רק בסוף מילוי ה TextBox, כאשר המשתמש עובר לשדה אחר.
השליטה במועד ביצוע הסנכרון נעשית באמצעות התכונה UpdateSourceTrigger של אובייקט ה Binding. האפשרויות לתכונה זו הם:
- PropertyChanged – העדכון יתבצע בכל שינוי בתכונת המקור.
- LostFocus – העדכון יתבצע רק כאשר הפקד יאבד את הפוקוס. אפשרות זו מצוינת כאשר מבצעים סנכרון בין שדה טקסטואלי לבין TextBox.
- Explicit – העדכון יבוצע אך ורק כאשר נריץ שורת קוד ב#C שתגרום לסנכרון. אפשרות זו טובה כאשר רוצים שליטה מלאה על זמן ביצוע הסנכרון.
בדוגמת הקוד הבאה ניתן לראות שימוש לשלושת השיטות:
<Window x:Class="WhenToUpdate.MainWindow"
>="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="350"
Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0"
Grid.Column="0"
Text="UpdateSourceTrigger"
FontWeight="Bold" />
<TextBlock Grid.Row="0"
Grid.Column="1"
Text="Data"
FontWeight="Bold" />
<TextBlock Grid.Row="0"
Grid.Column="2"
Text="UI"
FontWeight="Bold" />
<TextBlock Grid.Row="1"
Grid.Column="0"
Text="PropertyChanged" />
<TextBlock Grid.Row="2"
Grid.Column="0"
Text="LostFocus" />
<TextBlock Grid.Row="3"
Grid.Column="0"
Text="Explicit" />
<TextBlock x:Name="data1"
Grid.Row="1"
Grid.Column="1"
Text="text" />
<TextBlock x:Name="data2"
Grid.Row="2"
Grid.Column="1"
Text="text" />
<TextBlock x:Name="data3"
Grid.Row="3"
Grid.Column="1"
Text="text" />
<TextBox x:Name="ui1"
Grid.Row="1"
Grid.Column="2"
Text="{Binding ElementName=data1, Path=Text, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
<TextBox x:Name="ui2"
Grid.Row="2"
Grid.Column="2"
Text="{Binding ElementName=data2, Path=Text, UpdateSourceTrigger=LostFocus, Mode=TwoWay}" />
<TextBox x:Name="ui3"
Grid.Row="3"
Grid.Column="2"
Text="{Binding ElementName=data3, Path=Text, UpdateSourceTrigger=Explicit, Mode=TwoWay}" />
<Button Grid.Row="4"
Grid.Column="0"
Grid.ColumnSpan="3"
Content="Explicit"
Click="Button_Click" />
</Grid>
</Window>
קוד ה#C שמתלווה לתוכנית זו הוא:
using System.Windows;
using System.Windows.Controls;
namespace WhenToUpdate
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ui3.GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
}
}
שימו לב לשימוש בפקודה UpdateSource, זו הפקודה שגורמת לסנכרון ידני.
תוצאת הרצת התוכנית נראית כך:
כאשר נשנה את ה TextBox בשורה הראשונה (PropertyChanged) הData ישתנה בכל לחיצה. לעומת זאת, בשורה השנייה (LostFocus), הData ישתנה רק כאשר נעזוב את הTextBox. בשורה השלישית (Explicit) הData לא יתעדכן כל עוד לא לחצנו על הכפתור Explicit.
מומלץ לכתוב בעצמכם את התוכנית ולהתנסות בהבדלים.
תגובות בפייסבוק