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/04/27 16:46] smayr [Example 2] |
swdev:dotnet:wpf:dependency_properties [2013/01/25 09:16] (current) smayr [Example 1] |
||
---|---|---|---|
Line 57: | Line 57: | ||
DependencyObject source, DependencyPropertyChangedEventArgs e) | DependencyObject source, DependencyPropertyChangedEventArgs e) | ||
{ | { | ||
- | bool control = source as bool; | + | Button |
// Put some update logic here... | // Put some update logic here... | ||
- | | + | |
- | if (control == true) | + | if (control.IsCancel |
{ | { | ||
// do something | // do something | ||
Line 78: | 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 99: | 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 151: | Line 150: | ||
=== Example 2 === | === Example 2 === | ||
+ | A user control that requires a Dependency Property in order to set the '' | ||
+ | |||
+ | XAML: | ||
<code xml> | <code xml> | ||
- | < | ||
< | < | ||
. . . | . . . | ||
+ | < | ||
<ListBox Name=" | <ListBox Name=" | ||
| | ||
Line 177: | Line 179: | ||
</ | </ | ||
+ | C# code: | ||
<code csharp> | <code csharp> | ||
- | ... | + | using System; |
- | #region DependencyProperty SelectedItemIndex | + | using System.Collections.Generic; |
- | // | + | using System.Linq; |
- | // SelectedItemIndex | + | using System.Text; |
- | // | + | using System.Windows; |
- | // Dependency Property | + | using System.Windows.Controls; |
- | public static readonly DependencyProperty SelectedItemIndexProperty = | + | using System.Windows.Data; |
- | | + | using System.Windows.Documents; |
- | " | + | using System.Windows.Input; |
- | | + | using System.Windows.Media; |
- | typeof(ucActiveInstrument), | + | using System.Windows.Media.Imaging; |
- | new FrameworkPropertyMetadata( | + | using System.Windows.Navigation; |
- | new int(), | + | using System.Windows.Shapes; |
- | OnSelectedItemIndexPropertyChanged | + | |
- | ) | + | |
- | ); | + | |
- | // .NET Property Wrapper | + | namespace ACME.Controls |
- | public int SelectedItemIndex | + | |
{ | { | ||
- | | + | |
- | // called when you set the property from code. If you set the property from XAML | + | |
- | // the SetValue() method is called directly. | + | #region DependencyProperty SelectedItemIndex |
- | get { return | + | |
- | set { SetValue(SelectedItemIndexProperty, | + | // SelectedItemIndex |
- | } | + | // |
- | // Alias | + | // Dependency Property |
- | public | + | public static readonly DependencyProperty SelectedItemIndexProperty = |
- | { | + | DependencyProperty.Register( |
- | get { return SelectedItemIndex; } | + | " |
- | } | + | typeof(int), |
+ | typeof(ucActiveInstrument), | ||
+ | new FrameworkPropertyMetadata( | ||
+ | | ||
+ | | ||
+ | ) | ||
+ | ); | ||
- | /// | + | |
- | /// < | + | public int SelectedItemIndex |
- | /// OnPropertyChanged event handler for fitting view model. | + | { |
- | /// </ | + | // Important: Do not add any logic to these properties, because they are only |
- | /// <param name=" | + | // called when you set the property from code. If you set the property from XAML |
- | /// <param name=" | + | // the SetValue() method is called directly. |
- | /// | + | get { return (int)GetValue(SelectedItemIndexProperty); |
- | private static void OnSelectedItemIndexPropertyChanged( | + | set { SetValue(SelectedItemIndexProperty, |
- | | + | } |
- | DependencyPropertyChangedEventArgs e) | + | |
- | { | + | |
- | ucActiveInstrument control = source as ucActiveInstrument; | + | { |
- | // | + | |
+ | } | ||
- | | + | ///---------------------------------------------------------------------------------------- |
- | // Put some update logic here... | + | /// < |
- | // | + | /// OnPropertyChanged event handler for fitting view model. |
- | // | + | /// </ |
- | // | + | /// <param name=" |
- | control.lstOptions.SelectedIndex = (int)e.NewValue; | + | /// <param name=" |
+ | /// | ||
+ | private static void OnSelectedItemIndexPropertyChanged( | ||
+ | DependencyObject source, | ||
+ | DependencyPropertyChangedEventArgs e) | ||
+ | { | ||
+ | ucActiveInstrument control = source as ucActiveInstrument; | ||
+ | //DateTime time = (DateTime)e.NewValue; | ||
+ | |||
+ | // | ||
+ | | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | control.lstOptions.SelectedIndex = (int)e.NewValue; | ||
+ | } | ||
+ | # | ||
+ | |||
+ | // | ||
+ | /// < | ||
+ | /// 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; | ||
+ | } | ||
+ | |||
+ | } | ||
} | } | ||
- | #endregion | ||
</ | </ | ||
== See Also == | == See Also == | ||
* [[swdev: | * [[swdev: | ||
* [[http:// | * [[http:// |