Danh mục

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    
tailieu_vip

Phí tải xuống: 24,000 VND Tải xuống file đầy đủ (95 trang) 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 ...

Tài liệu được xem nhiều: