Pages

Sunday, 30 January 2011

Dependency Property

Definition:
Dependency property is a new type of property which can be used with dependency objects, to enable styling, data binding or animation. To define your own dependency property you have to register your new property using DependencyProperty.Register(...) method and optionally you can create FrameworkPropertyMetadata object where you can specify property characteristics.

Example:

   1:   public partial class MinMaxControl : Control
   2:      {
   3:          public static readonly DependencyProperty MinProperty = 
   4:              DependencyProperty.Register("Min", typeof(int), typeof(MinMaxControl),
   5:              new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, 
   6:                  HandleMinValueChanged, CoerceMinValue), ValidateMinValue);
   7:          public static readonly DependencyProperty MaxProperty = 
   8:              DependencyProperty.Register("Max", typeof(int), typeof(MinMaxControl),
   9:              new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
  10:                  HandleMaxValueChanged, CoerceMaxValue), ValidateMaxValue);
  11:   
  12:          static MinMaxControl()
  13:          {
  14:              DefaultStyleKeyProperty.OverrideMetadata(typeof(MinMaxControl), new FrameworkPropertyMetadata(typeof(MinMaxControl)));
  15:          }
  16:   
  17:          public int Min
  18:          {
  19:              get { return (int)GetValue(MinMaxControl.MinProperty); }
  20:              set { SetValue(MinMaxControl.MinProperty, value); }
  21:          }
  22:   
  23:          public int Max
  24:          {
  25:              get { return (int)GetValue(MinMaxControl.MaxProperty); }
  26:              set { SetValue(MinMaxControl.MaxProperty, value); }
  27:          }
  28:   

How to calculate coordinates of a control in WPF

If you have a control nested in another control and you have to calculate their coordinates, you can achieve it using TranslatePoint(..,..) method e.g. [your control].TranslatePoint(new Point(0, 0), [parent control]); If you need to retrieve a control coordinates to make it visible on the screen you can consider BringIntoView() method. The only problem with previously mentioned method is lack of consistency but on the other hand you do not have to perform any additional calculations.

Please have a look at the following example if you want to see TranslatePoint and BringIntoView methods in action: Coordinates.zip

Sunday, 9 January 2011

Saturday, 8 January 2011

How to restyle WPF controls

If you have to restyle some controls and you do not have a designer on-board there are some tools you can use or places you can check to find/gather some answers and inspirations. If you have Expression Blend you do not have to search for additional tools or help. Whatever you need to do it can be achieved with this tool. There are still some other solutions. You can use Microsoft's style examples which you can grab from Styling & Templating webpage or directly from this link ControlTemplateExamples.zip. If you are not satisfied with this approach you can go for Kaxaml which contains style snippets and can be used as a start point of your restyling process.

Expression Blend

Microsoft's examples 


Kaxaml