TAILIEUCHUNG - Illustrated WPF phần 7

Để thiết lập một DynamicResource trong các mã phía sau, sử dụng phương pháp SetResourceReference, như thể hiện trong các mã sau đây. Giống như FindResource, SetResourceReference tìm kiếm nguyên tố lên cây để tìm tài nguyên với chìa khóa nhất định. Các phương pháp SetResourceReference có hai thông số: • | CHAPTER 11 RESOURCES To set a DynamicResource in the code-behind use the SetResourceReference method as shown in the following code. Like FindResource SetResourceReference searches up the element tree to find the resource with the given key. The SetResourceReference method takes two parameters The first parameter is the name of the dependency property to which the resource is to be assigned. You must use the name of the dependency property and not the name of the CLR wrapper property. So in this case you must use Backgroundproperty and not Background. The second parameter is the resource key. BackgroundProperty gradBrush T T Dependency Resource Key Property Name To review suppose you have the markup shown on the left of Figure 11-8. Then the following code shows the three different forms for retrieving resources in C code first the direct assignment second the FindResource method and third the SetResourceReference method for assigning a DynamicResource. The first button uses the default Button properties and the last three buttons set their Background properties using the resource. public Window1 InitializeComponent background Brush background Brush background BackgroundProperty background Figure 11-8 shows a screenshot of the results on the right. StackPanel Name sp Button Name btnl Button 1 Button Button Name btn2 Button 2 Button Button Name btn3 Button 3 Button Button Name btn4 Button 4 Button StackPanel Figure 11-8. The output and the XAML of the sample program One last difference between StaticResources and DynamicResources is that StaticResources don t allow forward references in the XAML. This means that in the text of the XAML a resource must be defined before you can assign it to a property. DynamicResources don t have this restriction. 280 CHAPTER 11 RESOURCES Assembly Resources As I stated at the beginning of .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.