wpf - Custom DependencyProperty issue -


I did a test on custom dependency properties on WPF userControls, and it does not work ... please help.

Function:

Create a user-friendly bindable property when having a MyCircle - CenterX and CenterY .

This is my code:

MyTestCircle.xaml

  & lt; Canvas x: name = "main convection" width = "100" height = "100" & gt; & Lt; Ellipse x: Fill name = "myCircle" = "red" width = "{binding element name = main canvas, path = real person}" height = "{binding element name = main canvas, path = real high}" /> & Lt; / Canvas & gt;   MyTestCircle.xaml.cs   
  Public partial class MyTestCircle: UserControl {public MyTestCircle () {InitializeComponent (); } Public Double Center X {get {return (double) getValue (CentXXProperty); } Set {set value (center XPPERTY, value); }} Dependency for Public Stability Reading Property Center XProperty = Dependency Property. Control ("center", typef (double), typef (mitecartalk), new UIPprotamatadata (double .nan, new property changed callback (on secrecy switch), new cos Value Callback (Oncenter Xcore Value)); Public Static Zero OnCenterXChanged (dependent object obj, DependencyPropertyChangedEventArgs args) {MyTestCircle Circle = (MyTestCircle) obj; Canvas SetLeft (Circle, (double) args.NewValue - circle.Width / 2); } Public static object OnCenterXCoerceValue (dependent object source, object obj) {MyTestCircle Circle = (MyTestCircle) obj; Return canvas. Get left (circle) + circle. With / 2; } Public Double Center Y {get {return (double) getValue (CenterYProperty); } Set {Set Value (Center YProperty, value); }} Dependency Property Center YProperty = DependencyProperty.Register ("CenterY", Typef (DoubleType), MyTestCircle, New UIPropertyMetadata (Double.Nan, New Property Changeback Callback (On Syndication Change), New CosAvailability Callback for Public Stability Reading (On Sector YCoerceValue); Public Static Zero OnCenterYChanged (dependency object obj, DependencyPropertyChangedEventArgs args) {MyTestCircle Circle = (MyTestCircle) obv; canvas.sytop (circle, (double) args new value - circle.high / 2);} public Stable ob Select OnCenterYCoerceValue (Dependent Object Source, Object Obj) {MyTestCircle Circle = (MyTestCircle) obj; Return Canvas GateTop Circle Hight / 2;}}  

Test Canvas:

    line stroke =" black "x1 =" 0 "y1 =" 0 "x2 =" {binding element name = myTestCircle1, path = center X} "Y2 = "{Binding element name = myTestCircle1, path = center Y}" /> & Lt; / Canvas & gt;  

So, the test does not follow the center of the line circle in the canvas ... why?

Edit:

Note: In the code or designer, I never chanage CenterX < / Code> and CenterY in properties. I want the qualities to be "linked" with left / top canvas properties ... Is it possible?

You are never really setting the center and center so they live on their default but Other Problems The question seems to be homework, so I will only point out the problems.

Would like to adjust the price when used by it eg They do not need a price limited under this circumstance plus you have mistakenly entered the ozone which is a double for the matstatekarch.

Your change handler is using the width and height of MyTestCircle UserControl that are not set, you should either set them or set the size values, which are set. You can just have an oval bus in user control and set the size of UserControl.

If it is homework then try to fix the problems and we can get another look. If not, and you just want a line in the center, then I will completely substitute the properties using the underlying left, top properties and add a rendering form form which translates to -50, -50.

Do you need additional properties? If you set the left and the top, then do they need to update? If that is the case, then it will be a good solution.


Comments