Visual C# 2010 Recipes solution_8
Số trang: 95
Loại file: pdf
Dung lượng: 2.46 MB
Lượt xem: 11
Lượt tải: 0
Xem trước 10 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu visual c# 2010 recipes solution_8, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Visual C# 2010 Recipes solution_8 CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATION GetTemplateChild to find the button defined by its actual template. If this exists, it adds an event handler to the button’s Click event. The code for the control is as follows: using System.Windows; using System.Windows.Controls; using System.Windows.Markup; using Microsoft.Win32; namespace Apress.VisualCSharpRecipes.Chapter17 { [TemplatePart(Name = PART_Browse, Type = typeof(Button))] [ContentProperty(FileName)] public class FileInputControl : Control { static FileInputControl() { DefaultStyleKeyProperty.OverrideMetadata( typeof(FileInputControl), new FrameworkPropertyMetadata( typeof(FileInputControl))); } public override void OnApplyTemplate() { base.OnApplyTemplate(); Button browseButton = base.GetTemplateChild(PART_Browse) as Button; if (browseButton != null) browseButton.Click += new RoutedEventHandler(browseButton_Click); } void browseButton_Click(object sender, RoutedEventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); if (dlg.ShowDialog() == true) { this.FileName = dlg.FileName; } } public string FileName { get { return (string)GetValue(FileNameProperty); }830 CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATION set { SetValue(FileNameProperty, value); } } public static readonly DependencyProperty FileNameProperty = DependencyProperty.Register( FileName, typeof(string),typeof(FileInputControl)); }} The default style and control template for FileInputControl is in a ResourceDictionary in the Themessubfolder and is merged into the Generic ResourceDictionary. The XAML for this style is as follows: Browse... The XAML for the window that consumes this custom control is as follows: CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATION Title=Recipe17_14 Height=200 Width=300> 832 CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATIONFigure 17-11. Creating and using a FileInput custom control17-15. Create a Two-Way BindingProblemYou need to create a two-way binding so that when the value of either property changes, the other oneautomatically updates to reflect it.SolutionUse the System.Windows.Data.Binding markup extension, and set the Mode attribute to System.Windows.Data.BindingMode.TwoWay. Use the UpdateSourceTrigger attribute to specify when the binding sourceshould be updated.How It WorksThe data in a binding can flow from the source property to the target property, from the target propertyto the source property, or in both directions. For example, suppose the Text property of aSystem.Windows.Controls.TextBox control is bound to the Value property of a System.Windows.Controls.Slider control. In this case, the Text property of the TextBox control is the target of thebinding, and the Value property of the Slider control is the binding source. The direction of data flowbetween the target and the source can be configured in a number of different ways. It could beconfigured such that when the Value of the Slider control changes, the Text property of the TextBox isupdated. This is called a one-way binding. Alternatively, you could configure the binding so that whenthe Text property of the TextBox changes, the Slider control’s Value is automatically update ...
Nội dung trích xuất từ tài liệu:
Visual C# 2010 Recipes solution_8 CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATION GetTemplateChild to find the button defined by its actual template. If this exists, it adds an event handler to the button’s Click event. The code for the control is as follows: using System.Windows; using System.Windows.Controls; using System.Windows.Markup; using Microsoft.Win32; namespace Apress.VisualCSharpRecipes.Chapter17 { [TemplatePart(Name = PART_Browse, Type = typeof(Button))] [ContentProperty(FileName)] public class FileInputControl : Control { static FileInputControl() { DefaultStyleKeyProperty.OverrideMetadata( typeof(FileInputControl), new FrameworkPropertyMetadata( typeof(FileInputControl))); } public override void OnApplyTemplate() { base.OnApplyTemplate(); Button browseButton = base.GetTemplateChild(PART_Browse) as Button; if (browseButton != null) browseButton.Click += new RoutedEventHandler(browseButton_Click); } void browseButton_Click(object sender, RoutedEventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); if (dlg.ShowDialog() == true) { this.FileName = dlg.FileName; } } public string FileName { get { return (string)GetValue(FileNameProperty); }830 CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATION set { SetValue(FileNameProperty, value); } } public static readonly DependencyProperty FileNameProperty = DependencyProperty.Register( FileName, typeof(string),typeof(FileInputControl)); }} The default style and control template for FileInputControl is in a ResourceDictionary in the Themessubfolder and is merged into the Generic ResourceDictionary. The XAML for this style is as follows: Browse... The XAML for the window that consumes this custom control is as follows: CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATION Title=Recipe17_14 Height=200 Width=300> 832 CHAPTER 17 ■ WINDOWS PRESENTATION FOUNDATIONFigure 17-11. Creating and using a FileInput custom control17-15. Create a Two-Way BindingProblemYou need to create a two-way binding so that when the value of either property changes, the other oneautomatically updates to reflect it.SolutionUse the System.Windows.Data.Binding markup extension, and set the Mode attribute to System.Windows.Data.BindingMode.TwoWay. Use the UpdateSourceTrigger attribute to specify when the binding sourceshould be updated.How It WorksThe data in a binding can flow from the source property to the target property, from the target propertyto the source property, or in both directions. For example, suppose the Text property of aSystem.Windows.Controls.TextBox control is bound to the Value property of a System.Windows.Controls.Slider control. In this case, the Text property of the TextBox control is the target of thebinding, and the Value property of the Slider control is the binding source. The direction of data flowbetween the target and the source can be configured in a number of different ways. It could beconfigured such that when the Value of the Slider control changes, the Text property of the TextBox isupdated. This is called a one-way binding. Alternatively, you could configure the binding so that whenthe Text property of the TextBox changes, the Slider control’s Value is automatically update ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính tài liệu công nghệ thông tin lập trình máy tính mẹo máy tính cài đặt máy tínhGợi ý tài liệu liên quan:
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 317 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 305 0 0 -
Thêm chức năng hữu dụng cho menu chuột phải trên Windows
4 trang 290 0 0 -
70 trang 251 1 0
-
Bài giảng Tin học lớp 11 bài 1: Giới thiệu ngôn ngữ lập trình C#
15 trang 238 0 0 -
Tổng hợp lỗi Win 8 và cách sửa
3 trang 233 0 0 -
Sửa lỗi các chức năng quan trọng của Win với ReEnable 2.0 Portable Edition
5 trang 214 0 0 -
Giáo trình Bảo trì hệ thống và cài đặt phần mềm
68 trang 208 0 0 -
Tổng hợp 30 lỗi thương gặp cho những bạn mới sử dụng máy tính
9 trang 205 0 0 -
UltraISO chương trình ghi đĩa, tạo ổ đĩa ảo nhỏ gọn
10 trang 204 0 0