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/28 09:12]
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 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 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 182: Line 181:
 C# code: C# code:
 <code csharp> <code csharp>
-. . . +using System; 
-public partial class ucActiveInstrument : UserControl +using System.Collections.Generic; 
-{ +using System.Linq; 
-    #region DependencyProperty SelectedItemIndex +using System.Text; 
-    //----------------------------------- +using System.Windows; 
-    // SelectedItemIndex +using System.Windows.Controls; 
-    //----------------------------------- +using System.Windows.Data; 
-    // Dependency Property +using System.Windows.Documents; 
-    public static readonly DependencyProperty SelectedItemIndexProperty = +using System.Windows.Input; 
-      DependencyProperty.Register( +using System.Windows.Media; 
-          "SelectedItemIndex", +using System.Windows.Media.Imaging; 
-          typeof(int), +using System.Windows.Navigation; 
-          typeof(ucActiveInstrument), +using System.Windows.Shapes;
-          new FrameworkPropertyMetadata( +
-                new int(), +
-                OnSelectedItemIndexPropertyChanged +
-          ) +
-      );+
  
-    // .NET Property Wrapper +namespace ACME.Controls 
-    public int SelectedItemIndex+{ 
 +    public partial class ucActiveInstrument : UserControl
     {     {
-        // Important: Do not add any logic to these properties, because they are only  +        #region DependencyProperty SelectedItemIndex 
-        // called when you set the property from code. If you set the property from XAML  +        //----------------------------------- 
-        // the SetValue() method is called directly. +        // SelectedItemIndex 
-        get { return (int)GetValue(SelectedItemIndexProperty); } +        //----------------------------------- 
-        set { SetValue(SelectedItemIndexProperty, value); } +        // Dependency Property 
-    } +        public static readonly DependencyProperty SelectedItemIndexProperty = 
-    // Alias +          DependencyProperty.Register( 
-    public int SelectedItemIdx +              "SelectedItemIndex", 
-    { +              typeof(int)
-        get { return SelectedItemIndex; } +              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 code. If 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(SelectedItemIndexPropertyvalue); } 
-      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> 
-    } +        ///---------------------------------------------------------------------------------------- 
-    #endregion+        private static void OnSelectedItemIndexPropertyChanged( 
 +          DependencyObject source, 
 +          DependencyPropertyChangedEventArgs e) 
 +        
 +            ucActiveInstrument control = source as ucActiveInstrument
 +            //DateTime time = (DateTime)e.NewValue;
  
-    //----------------------------------------------------------------------------------------- +            //----------------------------------------- 
-    /// <summary> +            // Put some update logic here..
-    /// Constructor+            //----------------------------------------- 
-    /// </summary> +            //SetDataBinding((TFittingViewModel)e.NewValue, control); 
-    //----------------------------------------------------------------------------------------- +            //SetBinauralView(control); 
-    public ucActiveInstrument() +            control.lstOptions.SelectedIndex = (int)e.NewValue
-    { +        } 
-        InitializeComponent(); +        #endregion
-    }+
  
-    //----------------------------------------------------------------------------------------- +        //----------------------------------------------------------------------------------------- 
-    /// <summary> +        /// <summary> 
-    /// SelectionChange event handler for lstOptions+        /// Constructor
-    /// </summary> +        /// </summary> 
-    /// <param name="sender"></param> +        //----------------------------------------------------------------------------------------- 
-    /// <param name="e"></param> +        public ucActiveInstrument() 
-    //----------------------------------------------------------------------------------------- +        
-    private void lstOptions_SelectionChanged(object sender, SelectionChangedEventArgs e+            InitializeComponent()
-    +        }
-        this.SelectedItemIndex = lstOptions.SelectedIndex+
-    }+
  
 +        //-----------------------------------------------------------------------------------------
 +        /// <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;
 +        }
 +
 +    }
 } }
 </code>         </code>