{"id":64,"date":"2010-12-06T21:17:00","date_gmt":"2010-12-06T21:17:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2010\/12\/06\/introducing-custom-dependencyproperty-in-wpf\/"},"modified":"2010-12-06T21:17:00","modified_gmt":"2010-12-06T21:17:00","slug":"introducing-custom-dependencyproperty-in-wpf","status":"publish","type":"post","link":"http:\/\/panahy.nl\/index.php\/2010\/12\/06\/introducing-custom-dependencyproperty-in-wpf\/","title":{"rendered":"Introducing Custom DependencyProperty in WPF"},"content":{"rendered":"<p>In this sample code I want to introduce three DependencyProperty called Overlap Rotation and UpperOffset in a custom control OverlapStackPannel. The control is simply a StackPanel that arranges the containing items in a custom manner.<\/p>\n<p>In the ArrageOverride method I will use these three properties to locate each item as follows:<\/p>\n<p><!-- Start block. Created with Code4Blog for Microsoft Visual Studio 2010. Copyright (c)2010 Vitaly Zayko http:\/\/zayko.net --><\/p>\n<div style=\"color:black;overflow:auto;width:99.5%;\"><\/p>\n<pre style=\"margin:0em;\">   <span style=\"color:#0000ff\">protected<\/span><span style=\"color:#000000\">  <span style=\"color:#0000ff\">override<\/span><span style=\"color:#000000\">  System.Windows.<span style=\"color:#2b91af\">Size<\/span><span style=\"color:#000000\">  ArrangeOverride(System.Windows.<span style=\"color:#2b91af\">Size<\/span><span style=\"color:#000000\">  arrangeSize)<\/span><\/span><\/span><\/span><\/pre>\n<pre style=\"margin:0em;\">   <span style=\"color:#000000\">{<\/span><\/pre>\n<pre style=\"margin:0em;\">     <span style=\"color:#2b91af\">UIElementCollection<\/span><span style=\"color:#000000\">  children = <span style=\"color:#0000ff\">this<\/span><span style=\"color:#000000\">.InternalChildren;<\/span><\/span><\/pre>\n<pre style=\"margin:0em;\"><br \/><\/pre>\n<pre style=\"margin:0em;\">    <span style=\"color:#0000ff\">int<\/span><span style=\"color:#000000\">  childrenResolved = 0;<\/span><\/pre>\n<pre style=\"margin:0em;\">    <span style=\"color:#0000ff\">foreach<\/span><span style=\"color:#000000\">  (<span style=\"color:#2b91af\">UIElement<\/span><span style=\"color:#000000\">  child <span style=\"color:#0000ff\">in<\/span><span style=\"color:#000000\">  children)<\/span><\/span><\/span><\/pre>\n<pre style=\"margin:0em;\">    <span style=\"color:#000000\">{<\/span><\/pre>\n<pre style=\"margin:0em;\"> <span style=\"color:#2b91af\">Rect<\/span><span style=\"color:#000000\">  targetRect = <span style=\"color:#0000ff\">new<\/span><span style=\"color:#000000\">  <span style=\"color:#2b91af\">Rect<\/span><span style=\"color:#000000\"> ();<\/span><\/span><\/span><\/pre>\n<pre style=\"margin:0em;\"> targetRect.X = <span style=\"color:#0000ff\">this<\/span><span style=\"color:#000000\"> .Overlap * childrenResolved;<\/span><\/pre>\n<pre style=\"margin:0em;\"> targetRect.Y = <span style=\"color:#0000ff\">this<\/span><span style=\"color:#000000\"> .UpperOffset;<\/span><\/pre>\n<pre style=\"margin:0em;\"> targetRect.Width = child.DesiredSize.Width;<\/pre>\n<pre style=\"margin:0em;\"> targetRect.Height = child.DesiredSize.Height;<\/pre>\n<pre style=\"margin:0em;\"> child.Arrange(targetRect);<\/pre>\n<pre style=\"margin:0em;\"> child.RenderTransform = <span style=\"color:#0000ff\">new<\/span><span style=\"color:#000000\">  <span style=\"color:#2b91af\">RotateTransform<\/span><span style=\"color:#000000\"> (<span style=\"color:#0000ff\">this<\/span><span style=\"color:#000000\"> .Rotation);<\/span><\/span><\/span><\/pre>\n<pre style=\"margin:0em;\"> childrenResolved++;<\/pre>\n<pre style=\"margin:0em;\">    <span style=\"color:#000000\">}<\/span><\/pre>\n<pre style=\"margin:0em;\">    <span style=\"color:#0000ff\">return<\/span><span style=\"color:#000000\">  arrangeSize;<\/span><\/pre>\n<pre style=\"margin:0em;\">   <span style=\"color:#000000\">}<\/span><\/pre>\n<pre style=\"margin:0em;\"><br \/><\/pre>\n<\/div>\n<p><!-- End block --><br \/>To Achieve this I need to set the properties as follows:<\/p>\n<p><\/p>\n<div style=\"color:black;overflow:auto;width:99.5%;\">\n<pre style=\"margin:0em;\"> <span style=\"color:#0000ff\"> #region<\/span><span style=\"color:#000000\">  Property: UpperOffset<\/span><\/pre>\n<pre style=\"margin:0em;\">  <span style=\"color:#0000ff\">public<\/span><span style=\"color:#000000\">  <span style=\"color:#0000ff\">static<\/span><span style=\"color:#000000\">  <span style=\"color:#0000ff\">readonly<\/span><span style=\"color:#000000\">  <span style=\"color:#2b91af\">DependencyProperty<\/span><span style=\"color:#000000\">  UpperOffsetProperty = <span style=\"color:#2b91af\">DependencyProperty<\/span><span style=\"color:#000000\"> .Register(<span style=\"color:#a31515\">\"UpperOffset\"<\/span><span style=\"color:#000000\"> , <span style=\"color:#0000ff\">typeof<\/span><span style=\"color:#000000\"> (<span style=\"color:#0000ff\">double<\/span><span style=\"color:#000000\"> ), <span style=\"color:#0000ff\">typeof<\/span><span style=\"color:#000000\"> (<span style=\"color:#2b91af\">OverlapStackPanel<\/span><span style=\"color:#000000\"> ), <span style=\"color:#0000ff\">new<\/span><span style=\"color:#000000\">  <span style=\"color:#2b91af\">FrameworkPropertyMetadata<\/span><span style=\"color:#000000\"> (<span style=\"color:#0000ff\">double<\/span><span style=\"color:#000000\"> .NaN, <span style=\"color:#2b91af\">FrameworkPropertyMetadataOptions<\/span><span style=\"color:#000000\"> .AffectsArrange));<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/pre>\n<pre style=\"margin:0em;\">      <\/pre>\n<pre style=\"margin:0em;\">  <span style=\"color:#808080\">\/\/\/<span style=\"color:#008000\"> <span style=\"color:#808080\">&lt;summary&gt;<\/span><span style=\"color:#000000\"> <\/span><\/span><\/span><\/pre>\n<pre style=\"margin:0em;\">  <span style=\"color:#808080\">\/\/\/<span style=\"color:#008000\"> set or get the upper offset for each item in the pannel<\/span><span style=\"color:#000000\"> <\/span><\/span><\/pre>\n<pre style=\"margin:0em;\">  <span style=\"color:#808080\">\/\/\/<span style=\"color:#008000\"> <span style=\"color:#808080\">&lt;\/summary&gt;<\/span><span style=\"color:#000000\"> <\/span><\/span><\/span><\/pre>\n<pre style=\"margin:0em;\">  <span style=\"color:#0000ff\">public<\/span><span style=\"color:#000000\">  <span style=\"color:#0000ff\">double<\/span><span style=\"color:#000000\">  UpperOffset<\/span><\/span><\/pre>\n<pre style=\"margin:0em;\">  <span style=\"color:#000000\">{<\/span><\/pre>\n<pre style=\"margin:0em;\">    <span style=\"color:#0000ff\">get<\/span><span style=\"color:#000000\"> <\/span><\/pre>\n<pre style=\"margin:0em;\">    <span style=\"color:#000000\">{<\/span><\/pre>\n<pre style=\"margin:0em;\">      <span style=\"color:#0000ff\">return<\/span><span style=\"color:#000000\">  (<span style=\"color:#0000ff\">double<\/span><span style=\"color:#000000\"> )<span style=\"color:#0000ff\">this<\/span><span style=\"color:#000000\"> .GetValue(<span style=\"color:#2b91af\">OverlapStackPanel<\/span><span style=\"color:#000000\"> .UpperOffsetProperty);<\/span><\/span><\/span><\/span><\/pre>\n<pre style=\"margin:0em;\">    <span style=\"color:#000000\">}<\/span><\/pre>\n<pre style=\"margin:0em;\">    <span style=\"color:#0000ff\">set<\/span><span style=\"color:#000000\"> <\/span><\/pre>\n<pre style=\"margin:0em;\">    <span style=\"color:#000000\">{<\/span><\/pre>\n<pre style=\"margin:0em;\">      <span style=\"color:#0000ff\">this<\/span><span style=\"color:#000000\"> .SetValue(<span style=\"color:#2b91af\">OverlapStackPanel<\/span><span style=\"color:#000000\"> .UpperOffsetProperty, <span style=\"color:#0000ff\">value<\/span><span style=\"color:#000000\"> );<\/span><\/span><\/span><\/pre>\n<pre style=\"margin:0em;\">    <span style=\"color:#000000\">}<\/span><\/pre>\n<pre style=\"margin:0em;\">  <span style=\"color:#000000\">}<\/span><\/pre>\n<pre style=\"margin:0em;\"> <span style=\"color:#0000ff\"> #endregion<\/span><span style=\"color:#000000\"> <\/span><\/pre>\n<pre style=\"margin:0em;\"><span><span style=\"font-size: 16px; white-space: normal;\"><br \/><\/span><\/span><\/pre>\n<\/div>\n<p><!-- End block --><\/p>\n<p>and similar for other two.<br \/>This makes my OverlapStackPanel having the properties in Blend like this:<\/p>\n<div><a href=\"http:\/\/3.bp.blogspot.com\/_XurUeABuwSQ\/TP1ZHEA3hjI\/AAAAAAAADp0\/0TFHvWCkdrI\/s1600\/2010-12-06_2240.png\"><img style=\"display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 237px; height: 320px;\" src=\"http:\/\/3.bp.blogspot.com\/_XurUeABuwSQ\/TP1ZHEA3hjI\/AAAAAAAADp0\/0TFHvWCkdrI\/s320\/2010-12-06_2240.png\" border=\"0\" alt=\"\" id=\"BLOGGER_PHOTO_ID_5547688293861656114\" \/><\/a><\/p>\n<h3>Benefits<\/h3>\n<p>According to <a href='http:\/\/www.wpftutorial.net\/DependencyProperties.html' title='Christian Moser's WPF Tutorial'>this<\/a> nice article there are three main advantages of using DependencyProperties comparing to CLR properties:<\/p>\n<ul><\/p>\n<li>Reduced memory footprint<\/li>\n<p><\/p>\n<li>Value inheritance<\/li>\n<p><\/p>\n<li>Change notification<\/li>\n<p><\/ul>\n<p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this sample code I want to introduce three DependencyProperty called Overlap Rotation and UpperOffset in a custom control OverlapStackPannel. The control is simply a StackPanel that arranges the containing items in a custom manner. In the ArrageOverride method I will use these three properties to locate each item as follows: protected override System.Windows.Size ArrangeOverride(System.Windows.Size &hellip; <a href=\"http:\/\/panahy.nl\/index.php\/2010\/12\/06\/introducing-custom-dependencyproperty-in-wpf\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Introducing Custom DependencyProperty in WPF&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[61,51],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":0,"uagb_excerpt":"In this sample code I want to introduce three DependencyProperty called Overlap Rotation and UpperOffset in a custom control OverlapStackPannel. The control is simply a StackPanel that arranges the containing items in a custom manner. In the ArrageOverride method I will use these three properties to locate each item as follows: protected override System.Windows.Size ArrangeOverride(System.Windows.Size&hellip;","_links":{"self":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/64"}],"collection":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=64"}],"version-history":[{"count":0,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/64\/revisions"}],"wp:attachment":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}