{"id":89,"date":"2013-04-11T11:19:55","date_gmt":"2013-04-11T19:19:55","guid":{"rendered":"http:\/\/www.pgp-protector.com\/?p=89"},"modified":"2013-04-11T11:19:55","modified_gmt":"2013-04-11T19:19:55","slug":"binding-and-dependencyproperty","status":"publish","type":"post","link":"http:\/\/www.pgp-protector.com\/?p=89","title":{"rendered":"Binding and DependencyProperty"},"content":{"rendered":"<p>Long story short.<br \/>\nI was trying to create a custom object that I can bind <strong>to<\/strong>, not bind from.<\/p>\n<p>So off to google dealing with WPF, Binding, UserControls, ect&#8230;.<\/p>\n<p>Lots of results on how to bind a variable to a text box, data table , list to drop boxes and all that, but What I was wanting to do was a bit different.<\/p>\n<p>I wanted to use my own control that draws a &#8220;Target&#8221; on a canvas map, and allows you to Move the Target just by updating the Location(a System.Windows.Point) should be simple right?<br \/>\nWell it is after you figure out what you&#8217;re doing \ud83d\ude42<\/p>\n<p>My Thread that generates the X,Y\u00a0coordinates for a Robot Map was running find, and the X &amp; Y Values were bound to a text box on the main form, so I know it&#8217;s running fine in the background.<br \/>\nAny time it changes the X Or Y Value, it would also update the Location Point, and send a NotifyPropertyChanged(&#8220;CurrentLocation&#8221;); to boot. \u00a0That was working fine also.<\/p>\n<p>But how to get my control to receive it?????<\/p>\n<p>There were two problems.<br \/>\n1) it wasn&#8217;t a single variable I&#8217;m trying to get, but a Point object<br \/>\n2) how to allow it to Receive the Binding, not Set a binding.<\/p>\n<p>So I give you the solution that I found, (And it works for me) though I&#8217;m sure it needs\u00a0improvements.<\/p>\n<p>&nbsp;<\/p>\n<pre>using System.ComponentModel;\r\nusing System.Windows;\r\nusing System.Windows.Controls;\r\nusing System.Windows.Media;\r\nusing System.Windows.Shapes;\r\n\r\nnamespace MyApplication\r\n{\r\n  public class TargetCurser : UserControl\r\n  {\r\n    public static readonly DependencyProperty TargetProperty = DependencyProperty.Register(\"MyPoint\", typeof(Point), typeof(TargetCurser),new PropertyMetadata(new Point(0,0),OnTargetPropertyChanged));\r\n    private Point _MyPoint = new Point(0, 0);\r\n    public Point MyPoint { get { return _MyPoint; } set { _MyPoint = value; MoveTo(MyPoint); } }\r\n    private static void OnTargetPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)\r\n      {\r\n        TargetCurser control = sender as TargetCurser;\r\n        if (control != null) { control.MyPoint = (Point)e.NewValue;}\r\n      }\r\n    private Path TargetObject = null;\r\n    private Canvas TargetsCanvas = null;\r\n    private Path TObject(string ObjectName, double Size, Color TargetColor)\r\n    {\r\n       \/\/ Snip COde, just build a Path of what you want and store it in LinePath to return.\r\n       return LinePath;\r\n    }\r\n    private void MoveTo(Point NewLocation)\r\n      {\r\n      if (TargetsCanvas == null) return; \/\/ Can't move if we're not given a valid surface to move on.\r\n      if (TargetObject == null) return; \/\/ Can't Move if we haven't build the TargetObject\r\n      Path FindTargetOnCanvas = TargetsCanvas.FindName(TargetObject.Name) as Path;\r\n      if (FindTargetOnCanvas == null) return; \/\/ Unable to Find Target on Path, can't move it.\r\n      FindTargetOnCanvas.SetValue(Canvas.TopProperty, NewLocation.Y * ScalingFactor);\r\n      FindTargetOnCanvas.SetValue(Canvas.LeftProperty, NewLocation.X * ScalingFactor);\r\n      }\r\n    public TargetCurser(Canvas DrawOn, string TargetsName)\r\n      {\r\n      UIElement ExistCheck = DrawOn.FindName(TargetsName) as UIElement;\r\n      if (ExistCheck != null)\r\n      {\r\n        DrawOn.UnregisterName(TargetsName);\r\n        DrawOn.Children.Remove(ExistCheck);\r\n      }\r\n\r\n      TargetObject = TObject(TargetsName, 5, Colors.Red);\r\n      TargetsCanvas = DrawOn;\r\n      TargetsCanvas.RegisterName(TargetObject.Name, TargetObject);\r\n      TargetsCanvas.Children.Add(TargetObject);\r\n      MoveTo(MyPoint);\r\n      }\r\n    }\r\n}<\/pre>\n<p>Now for a bit of explaining.<\/p>\n<p>1) You don&#8217;t need the\u00a0INotifyPropertyChanged bit of code for a control that will be RECEIVING the Binding unless you&#8217;re also changing values and wish to send it back out.<br \/>\n2)\u00a0public static readonly DependencyProperty TargetProperty = DependencyProperty.Register(&#8220;MyPoint&#8221;, typeof(Point), typeof(TargetCurser),new PropertyMetadata(new Point(0,0),OnTargetPropertyChanged));<br \/>\nThis is where I was having the trouble, It would &#8220;Bind&#8221; tell I modified the last part PropertyMetadata(Default Value, WhatToDo)<br \/>\nGiven my object is expecting a Point, I used new Point(0,0) vs a Null or 0 or &#8220;&#8221; like in some of the examples I saw on the net.<br \/>\nand the WhatToDo \/ OnTargetPropertyChanged (What ever void Property you&#8217;re wanting called when the variable bound to this changes);<br \/>\n3) private static void WhatToDo(DependencyObject sender, DependencyPropertyChangedEventArgs e)<br \/>\nThis is where you&#8217;ll process any data that is Sent to your object \/ class when the Data Bound to your class changes, the new &amp; old value will be in &#8220;e&#8221; (PropertyChangedEventArgs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Long story short. I was trying to create a custom object that I can bind to, not bind from. So off to google dealing with WPF, Binding, UserControls, ect&#8230;. Lots of results on how to bind a variable to a text box, data table , list to drop boxes and all that, but What I [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"http:\/\/www.pgp-protector.com\/index.php?rest_route=\/wp\/v2\/posts\/89"}],"collection":[{"href":"http:\/\/www.pgp-protector.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.pgp-protector.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.pgp-protector.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.pgp-protector.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=89"}],"version-history":[{"count":3,"href":"http:\/\/www.pgp-protector.com\/index.php?rest_route=\/wp\/v2\/posts\/89\/revisions"}],"predecessor-version":[{"id":92,"href":"http:\/\/www.pgp-protector.com\/index.php?rest_route=\/wp\/v2\/posts\/89\/revisions\/92"}],"wp:attachment":[{"href":"http:\/\/www.pgp-protector.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.pgp-protector.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.pgp-protector.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}