Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
swdev:dotnet:wpf:dependency_properties [2011/02/08 08:55] smayr |
swdev:dotnet:wpf:dependency_properties [2013/01/25 09:16] (current) smayr [Example 1] |
||
---|---|---|---|
Line 18: | Line 18: | ||
typeof(bool), | typeof(bool), | ||
new FrameworkPropertyMetadata(false, | new FrameworkPropertyMetadata(false, | ||
- | new PropertyChangedCallback(OnIsDefaultChanged))); | + | new PropertyChangedCallback(OnIsCancelPropertyChanged))); |
… | … | ||
} | } | ||
Line 30: | Line 30: | ||
| | ||
// A property changed callback (not must) | // A property changed callback (not must) | ||
- | private static void OnIsCancelChanged( | + | private static void OnIsCancelPropertyChanged( |
DependencyObject o, DependencyPropertyChangedEventArgs e) { … } | DependencyObject o, DependencyPropertyChangedEventArgs e) { … } | ||
+ | … | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Alternatively: | ||
+ | <code csharp> | ||
+ | public class Button : ButtonBase | ||
+ | { | ||
+ | // The dependency property declaration, | ||
+ | public static readonly DependencyProperty IsCancelProperty = DependencyProperty.Register(“IsCancel”, | ||
+ | typeof(bool), | ||
+ | new FrameworkPropertyMetadata(false, | ||
+ | new PropertyChangedCallback(OnIsCancelPropertyChanged))); | ||
+ | | ||
+ | // A .NET property wrapper (not must) | ||
+ | public bool IsCancel | ||
+ | { | ||
+ | get { return (bool)GetValue(Button.IsCancelProperty ); } | ||
+ | set { SetValue(Button.IsCancelProperty , value); } | ||
+ | } | ||
+ | | ||
+ | // A property changed callback (not must) | ||
+ | private static void OnIsCancelPropertyChanged( | ||
+ | DependencyObject source, DependencyPropertyChangedEventArgs e) | ||
+ | { | ||
+ | | ||
+ | |||
+ | // Put some update logic here... | ||
+ | | ||
+ | if (control.IsCancel == true) | ||
+ | { | ||
+ | // do something | ||
+ | | ||
+ | } | ||
+ | | ||
+ | // Constructor | ||
+ | static Button() | ||
+ | { | ||
+ | } | ||
+ | | ||
… | … | ||
} | } | ||
Line 38: | Line 78: | ||
IMPORTANT: The accessor property is fine to use, but the XAML code will only call the SetValue() and GetValue() methods directly, so make sure that no other code gets added to the accessor property, to make sure both work the same. | IMPORTANT: The accessor property is fine to use, but the XAML code will only call the SetValue() and GetValue() methods directly, so make sure that no other code gets added to the accessor property, to make sure both work the same. | ||
- | === Example === | + | === Example |
<code csharp> | <code csharp> | ||
public class Employee : DependencyObject | public class Employee : DependencyObject | ||
Line 59: | Line 99: | ||
// If employee id is greater than 1000 then make it 1000 | // If employee id is greater than 1000 then make it 1000 | ||
- | static void EmpIdCallBack(DependencyObject d, | + | static void EmpIdCallBack(DependencyObject d, DependencyPropertyChangedEventArgs args) |
- | | + | |
{ | { | ||
Employee emp = (Employee)d; | Employee emp = (Employee)d; | ||
Line 109: | Line 148: | ||
Source: [[http:// | Source: [[http:// | ||
+ | === Example 2 === | ||
+ | |||
+ | A user control that requires a Dependency Property in order to set the '' | ||
+ | |||
+ | XAML: | ||
+ | <code xml> | ||
+ | < | ||
+ | . . . | ||
+ | < | ||
+ | <ListBox Name=" | ||
+ | | ||
+ | < | ||
+ | < | ||
+ | <Image Source="/ | ||
+ | </ | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | <Image Source="/ | ||
+ | </ | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | <Image Source="/ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | C# code: | ||
+ | <code csharp> | ||
+ | using System; | ||
+ | using System.Collections.Generic; | ||
+ | using System.Linq; | ||
+ | using System.Text; | ||
+ | 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 ACME.Controls | ||
+ | { | ||
+ | public partial class ucActiveInstrument : UserControl | ||
+ | { | ||
+ | #region DependencyProperty SelectedItemIndex | ||
+ | // | ||
+ | // SelectedItemIndex | ||
+ | // | ||
+ | // Dependency Property | ||
+ | public static readonly DependencyProperty SelectedItemIndexProperty = | ||
+ | DependencyProperty.Register( | ||
+ | " | ||
+ | typeof(int), | ||
+ | typeof(ucActiveInstrument), | ||
+ | new FrameworkPropertyMetadata( | ||
+ | new int(), | ||
+ | OnSelectedItemIndexPropertyChanged | ||
+ | ) | ||
+ | ); | ||
+ | |||
+ | // .NET Property Wrapper | ||
+ | public int SelectedItemIndex | ||
+ | { | ||
+ | // Important: Do not add any logic to these properties, because they are only | ||
+ | // called when you set the property from code. If you set the property from XAML | ||
+ | // the SetValue() method is called directly. | ||
+ | get { return (int)GetValue(SelectedItemIndexProperty); | ||
+ | set { SetValue(SelectedItemIndexProperty, | ||
+ | } | ||
+ | // Alias | ||
+ | public int SelectedItemIdx | ||
+ | { | ||
+ | get { return SelectedItemIndex; | ||
+ | } | ||
+ | |||
+ | /// | ||
+ | /// < | ||
+ | /// OnPropertyChanged event handler for fitting view model. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | /// <param name=" | ||
+ | /// | ||
+ | private static void OnSelectedItemIndexPropertyChanged( | ||
+ | DependencyObject source, | ||
+ | DependencyPropertyChangedEventArgs e) | ||
+ | { | ||
+ | ucActiveInstrument control = source as ucActiveInstrument; | ||
+ | //DateTime time = (DateTime)e.NewValue; | ||
+ | |||
+ | // | ||
+ | // Put some update logic here... | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | control.lstOptions.SelectedIndex = (int)e.NewValue; | ||
+ | } | ||
+ | #endregion | ||
+ | |||
+ | // | ||
+ | /// < | ||
+ | /// Constructor. | ||
+ | /// </ | ||
+ | // | ||
+ | public ucActiveInstrument() | ||
+ | { | ||
+ | InitializeComponent(); | ||
+ | } | ||
+ | |||
+ | // | ||
+ | /// < | ||
+ | /// SelectionChange event handler for lstOptions. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | /// <param name=" | ||
+ | // | ||
+ | private void lstOptions_SelectionChanged(object sender, SelectionChangedEventArgs e) | ||
+ | { | ||
+ | this.SelectedItemIndex = lstOptions.SelectedIndex; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | } | ||
+ | </ | ||
== See Also == | == See Also == | ||
* [[swdev: | * [[swdev: | ||
* [[http:// | * [[http:// |