Chuyển đến nội dung chính

Bài đăng

Hiển thị các bài đăng có nhãn WPF

ListBox Multi Select

public static class ListBoxSelectedItems { private static readonly DependencyProperty SelectedItemsBehaviorProperty = DependencyProperty.RegisterAttached( "SelectedItemsBehavior" , typeof(SelectedItemsBehavior), typeof(ListBox), null); public static readonly DependencyProperty ItemsProperty = DependencyProperty.RegisterAttached( "Items" , typeof(IList), typeof(ListBoxSelectedItems), new PropertyMetadata(null, ItemsPropertyChanged)); public static void SetItems(ListBox listBox, IList list) {listBox.SetValue(ItemsProperty, list); } public static IList GetItems(ListBox listBox) { return listBox.GetValue(ItemsProperty) as IList; } private static void ItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {...

WPF: Visual Tree và TreeView

Khi bạn tìm hiểu về thiết kế giao diện bên WPF, có 2 thành phần quan trọng mà bạn cần chú ý, đó là Logical Tree và Visual Tree. Hôm nay mình bàn về Visual Tree Gần như các thành phần được hiển thị trên window được gọi là visual tree. Bạn có thể nghĩ rằng visual tree được mở rộng từ logical tree, trong đó, ngoài các thành phần chính trong logical tree, nó còn có thành phần khác như border, scrollview... Dưới đây là hình ví dụ về Visual Tree, trong đó các node được tô đậm là Logical Tree. Duyệt cây Visual Tree private void PrintVisualTree(int depth, DependencyObject obj) { Debug.WriteLine(new string(' ', depth) + obj); //txtKq.Text = txtKq.Text + "\n" + new string(' ', depth) + obj; for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { PrintVisualTree(depth + 1, VisualTreeHelper.GetChild(obj, i)); } }

Sử dụng Themes trong WPF

Bạn đang sử dụng WPF và muốn ứng dụng của mình viết ra thật bóng bẩy và đẹp. Nhưng việc thiết kế giao diện WPF sao cho thật đẹp thì rất khó. Cách đơn giản nhất là sử dụng Theme có sẵn ở trên mạng :D

Data Binding with INotifyPropertyChanged

Làm sao để cập nhật trạng thái, dữ liệu trên giao diện phần mềm nếu phần mềm của bạn chạy đa luồng (Multithreading). Cách đơn giản nhất là dùng Data binding, mà ví dụ của tôi ở đây là sử dụng lớp INotifyPropertyChanged. Namespace : System.ComponentModel; Lớp interface này có duy nhất 1 thành viên là public event PropertyChangedEventHandler PropertyChanged ; Để thực thi interface này, bạn cần implement phương thức PropertyChanged.