- XAML - Custom Controls
- XAML - Debugging
- XAML - Triggers
- XAML - Styles
- XAML - Templates
- XAML - Resources
- XAML - Dependency Properties
- XAML - Markup Extensions
- XAML - Data Binding
- XAML - Event Handling
- XAML - Layouts
- XAML - Controls
- XAML - Building Blocks
- XAML Vs.VB.NET
- XAML Vs C# Code
- Writing XAML Aplication On MAC OS
- XAML - Environment Setup
- XAML - Overview
- XAML - Home
XAML Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
XAML - Dependency Properties
A dependency property is a specific type of property where the value is followed by a keen property system which is also a part of the Windows Runtime App. A class which defines a dependency property must be inherited from the DependencyObject class.
Many of the UI control classes which are used in XAML are derived from the DependencyObject class and support dependency properties. The following XAML code creates a button with some properties.
<Window x:Class = "XAMLDependencyProperty.MainWindow" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local = "clr-namespace:XAMLDependencyProperty" Title = "MainWindow" Height = "350" Width = "604"> <Grid> <Button Height = "40" Width = "175" Margin = "10" Content = "Dependency Property"> <Button.Style> <Style TargetType = "{x:Type Button}"> <Style.Triggers> <Trigger Property = "IsMouseOver" Value = "True"> <Setter Property = "Foreground" Value = "Red" /> </Trigger> </Style.Triggers> </Style> </Button.Style> </Button> </Grid> </Window>
The x:Type markup extension in XAML has a similar functionapty pke typeof() in C#. It is used when attributes are specified that take the type of the object such as <Style TargetType = "{x:Type Button}">
When you compile and execute the above code, it will produce the following MainWindow. When the mouse is over the button, it will change the foreground color of the button. When the mouse leaves the button, it will change back to its original color.
The main difference between dependency properties and other CLR properties are −
CLR properties can directly read/write from the private member of a class by using getter and setter. In case of dependency properties, it is not stored in a local object.
Dependency properties are stored in a dictionary of key/value pairs which is provided by the DependencyObject class.
It also saves a lot of memory because it stores the property when changed.
It can be bound in XAML as well.
In .NET framework, custom dependency properties can also be defined. Here are the steps to define custom dependency property in C#.
Declare and register your dependency property with system call register.
Provide the setter and getter for the property.
Define a static handler to handle any changes that occur globally.
Define an instance handler to handle any changes that occur to that particular instance.
Given below is the code in C# for dependency property which defined to set the SetText property of the user control.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApppcation3 { /// <summary> /// Interaction logic for UserControl1.xaml /// </summary> pubpc partial class UserControl1 : UserControl { pubpc UserControl1() { InitiapzeComponent(); } pubpc static readonly DependencyProperty SetTextProperty = DependencyProperty.Register("SetText", typeof(string), typeof(UserControl1), new PropertyMetadata("", new PropertyChangedCallback(OnSetTextChanged))); pubpc string SetText { get {return(string) GetValue(SetTextProperty); } set {SetValue(SetTextProperty, value);} } private static void OnSetTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { UserControl1 UserControl1Control = d as UserControl1; UserControl1Control.OnSetTextChanged(e); } private void OnSetTextChanged(DependencyPropertyChangedEventArgs e) { tbTest.Text = e.NewValue.ToString(); } } }
Here is the XAML file in which the TextBlock is defined as a user control and the Text property will be assigned to it by the SetText dependency property.
The following XAML code creates a user control with initiapzing its SetText dependency property and some other properties.
<Window x:Class = "WpfApppcation3.MainWindow" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:views = "clr-namespace:WpfApppcation3" Title = "MainWindow" Height = "350" Width = "604"> <Grid> <views:UserControl1 SetText = "Hellow World" /> </Grid> </Window>
Let s run this apppcation and you can see immediately in our MainWindow that dependency property for user control has been successfully used as a Text.
Advertisements