home screen

Search



Number Of Result : 0

Result :


Tuesday, October 28, 2008

UpdateSource các phần tử con của 1 UIElement

1 . METHOD
//hàm chính dùng để update


public static void UpdateSource(this UIElement control)
{
PrintLogicalTree(0, control);
}



//duyệt phần tử con của obj
private static void PrintLogicalTree(int depth, object obj)
{
// Sometimes leaf nodes aren’t DependencyObjects (e.g. strings)
if (!(obj is DependencyObject)) return;

// Recursive call for each logical child
FrameworkElement element = obj as FrameworkElement;
if (element != null)
{

//Get dependency Property
DependencyProperty dp = FactoryDependencyPropertyCommon(element);
if (dp != null)
{
BindingExpression be = element.GetBindingExpression(dp);
if (be != null)
be.UpdateSource();
}
}

foreach (object child in LogicalTreeHelper.GetChildren(obj as DependencyObject))
PrintLogicalTree(depth + 1, child);
}




//lay DependencyProperty của từng loại UIElement khi Binding
private static DependencyProperty FactoryDependencyPropertyCommon(FrameworkElement element)
{
if (element.GetType() == typeof(TextBox))
return TextBox.TextProperty;
else if (element.GetType() == typeof(CheckBox))
return CheckBox.IsCheckedProperty;
return null;

}




2 . Usage
//KHỞI TẠO
StackPanel panel .....
//đưa các phần tử con vào trong panel

---------------------------------------------

//SAVE đối tượng
//trước khi save thực hiện lện updateSource
panel.UpdateSource();

//Save đối tượng xuống DB


3 . WHY
trong quá trinh binding chúng ta chỉ binding 2 chiều với UpdateSourceTrigger=Explicit, khi đó sử dụng phương thức này để thực hiện cập nhât các phân tử đã binding và thây đổi trong quá trình người dùng

No comments: