Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
swdev:dotnet:wpf:dependency_properties [2011/04/27 16:44]
smayr [Example]
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 control = source as Button;
    
        // Put some update logic here...        // Put some update logic here...
-       control = (bool)e.NewValue; +       control.IsCancel = (bool)e.NewValue; 
-       if (control == true)+       if (control.IsCancel == true)
        {        {
            // 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)
-        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 ''SelectedIndex'' in a ''ListBox'' using Data Binding.
 +
 +XAML:
 +<code xml>
 +<UserControl x:Class="ACME.Controls.ucActiveInstrument". . .>
 +. . .
 +<Grid>
 +        <ListBox Name="lstOptions" Width="110" Height="40" SelectedIndex="1"
 +                 SelectionChanged="lstOptions_SelectionChanged">
 +            <ListBoxItem>
 +                <ListBoxItem.Content>
 +                    <Image Source="/images/icons/icon-device-right.png" Height="25" />
 +                </ListBoxItem.Content>
 +            </ListBoxItem>
 +            <ListBoxItem>
 +                <ListBoxItem.Content>
 +                    <Image Source="/images/icons/icon-device-both.png" Height="25" />
 +                </ListBoxItem.Content>
 +            </ListBoxItem>
 +            <ListBoxItem>
 +                <ListBoxItem.Content>
 +                    <Image Source="/images/icons/icon-device-left.png" Height="25" />
 +                </ListBoxItem.Content>
 +            </ListBoxItem>
 +        </ListBox>
 +    </Grid>
 +</UserControl>
 +</code>
 +
 +C# code:
 <code csharp> <code csharp>
-#region DependencyProperty SelectedItemIndex +using System; 
-//----------------------------------- +using System.Collections.Generic; 
-// SelectedItemIndex +using System.Linq; 
-//----------------------------------- +using System.Text; 
-// Dependency Property +using System.Windows; 
-public static readonly DependencyProperty SelectedItemIndexProperty = +using System.Windows.Controls; 
-  DependencyProperty.Register( +using System.Windows.Data; 
-      "SelectedItemIndex", +using System.Windows.Documents; 
-      typeof(int), +using System.Windows.Input; 
-      typeof(ucActiveInstrument), +using System.Windows.Media; 
-      new FrameworkPropertyMetadata( +using System.Windows.Media.Imaging; 
-            new int(), +using System.Windows.Navigation; 
-            OnSelectedItemIndexPropertyChanged +using System.Windows.Shapes;
-      ) +
-  );+
  
-// .NET Property Wrapper +namespace ACME.Controls
-public int SelectedItemIndex+
 { {
-    // ImportantDo not add any logic to these properties, because they are only  +    public partial class ucActiveInstrument UserControl 
-    // 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 (int)GetValue(SelectedItemIndexProperty); } +        //----------------------------------- 
-    set { SetValue(SelectedItemIndexProperty, value); } +        // SelectedItemIndex 
-+        //----------------------------------- 
-// Alias +        // Dependency Property 
-public int SelectedItemIdx +        public static readonly DependencyProperty SelectedItemIndexProperty = 
-{ +          DependencyProperty.Register( 
-    get { return SelectedItemIndex+              "SelectedItemIndex", 
-}+              typeof(int)
 +              typeof(ucActiveInstrument), 
 +              new FrameworkPropertyMetadata
 +                    new int(), 
 +                    OnSelectedItemIndexPropertyChanged 
 +              ) 
 +          );
  
-///---------------------------------------------------------------------------------------- +        // .NET Property Wrapper 
-/// <summary> +        public int SelectedItemIndex 
-/// OnPropertyChanged event handler for fitting view model+        { 
-/// </summary> +            // Important: Do not add any logic to these properties, because they are only  
-/// <param name="source">Source control</param> +            // called when you set the property from codeIf you set the property from XAML  
-/// <param name="e">Event Arguments</param> +            // the SetValue() method is called directly. 
-///---------------------------------------------------------------------------------------- +            get { return (int)GetValue(SelectedItemIndexProperty); } 
-private static void OnSelectedItemIndexPropertyChanged( +            set { SetValue(SelectedItemIndexProperty, value); } 
-  DependencyObject source, +        } 
-  DependencyPropertyChangedEventArgs e) +        // Alias 
-{ +        public int SelectedItemIdx 
-    ucActiveInstrument control = source as ucActiveInstrument+        { 
-    //DateTime time = (DateTime)e.NewValue;+            get return SelectedItemIndex} 
 +        }
  
-    //----------------------------------------- +        ///---------------------------------------------------------------------------------------- 
-    // Put some update logic here... +        /// <summary> 
-    //----------------------------------------- +        /// OnPropertyChanged event handler for fitting view model. 
-    //SetDataBinding((TFittingViewModel)e.NewValue, control); +        /// </summary> 
-    //SetBinauralView(control); +        /// <param name="source">Source control</param> 
-    control.lstOptions.SelectedIndex = (int)e.NewValue;+        /// <param name="e">Event Arguments</param> 
 +        ///---------------------------------------------------------------------------------------- 
 +        private static void OnSelectedItemIndexPropertyChanged( 
 +          DependencyObject source, 
 +          DependencyPropertyChangedEventArgs e) 
 +        { 
 +            ucActiveInstrument control = source as ucActiveInstrument; 
 +            //DateTime time = (DateTime)e.NewValue; 
 + 
 +            //----------------------------------------- 
 +            // Put some update logic here... 
 +            //----------------------------------------- 
 +            //SetDataBinding((TFittingViewModel)e.NewValue, control); 
 +            //SetBinauralView(control); 
 +            control.lstOptions.SelectedIndex = (int)e.NewValue; 
 +        } 
 +        #endregion 
 + 
 +        //----------------------------------------------------------------------------------------- 
 +        /// <summary> 
 +        /// Constructor. 
 +        /// </summary> 
 +        //----------------------------------------------------------------------------------------- 
 +        public ucActiveInstrument() 
 +        { 
 +            InitializeComponent(); 
 +        } 
 + 
 +        //----------------------------------------------------------------------------------------- 
 +        /// <summary> 
 +        /// SelectionChange event handler for lstOptions. 
 +        /// </summary> 
 +        /// <param name="sender"></param> 
 +        /// <param name="e"></param> 
 +        //----------------------------------------------------------------------------------------- 
 +        private void lstOptions_SelectionChanged(object sender, SelectionChangedEventArgs e) 
 +        { 
 +            this.SelectedItemIndex = lstOptions.SelectedIndex; 
 +        } 
 + 
 +    }
 } }
-#endregion 
 </code>         </code>        
 == See Also == == See Also ==
   * [[swdev:dotnet:wpf:implementing_mvvm|Implementing M-V-VM]]   * [[swdev:dotnet:wpf:implementing_mvvm|Implementing M-V-VM]]
   * [[http://zamjad.wordpress.com/2009/07/13/dependency-property-metadata/|Zeeshan Amjad: Dependency Property Metadata]]   * [[http://zamjad.wordpress.com/2009/07/13/dependency-property-metadata/|Zeeshan Amjad: Dependency Property Metadata]]