Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1106305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:46:17+00:00 2026-05-17T01:46:17+00:00

I did a test on custom dependency properties on WPF userControls, and does not

  • 0

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

Task:

Having an UserControl – MyCircle, build the CenterX and CenterY bindable properties.

Here is my code:

MyTestCircle.xaml

<Canvas x:Name="mainCanvas" Width="100" Height="100">
    <Ellipse x:Name="myCircle" Fill="Red" 
             Width="{Binding ElementName=mainCanvas, Path=ActualWidth}"
             Height="{Binding ElementName=mainCanvas, Path=ActualHeight}"/>     
</Canvas>

MyTestCircle.xaml.cs

public partial class MyTestCircle : UserControl
{
    public MyTestCircle()
    {
        InitializeComponent();
    }

    public double CenterX
    {
        get { return (double)GetValue(CenterXProperty); }
        set { SetValue(CenterXProperty, value); }
    }

    public static readonly DependencyProperty CenterXProperty =
        DependencyProperty.Register("CenterX", typeof(double), typeof(MyTestCircle),
        new UIPropertyMetadata(double.NaN, 
            new PropertyChangedCallback(OnCenterYChanged), 
            new CoerceValueCallback(OnCenterXCoerceValue)));

    public static void OnCenterXChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        MyTestCircle circle = (MyTestCircle)obj;
        Canvas.SetLeft(circle, (double)args.NewValue - circle.Width / 2);
    }

    public static object OnCenterXCoerceValue(DependencyObject source, object obj)
    {
        MyTestCircle circle = (MyTestCircle)obj;
        return Canvas.GetLeft(circle) + circle.Width / 2;            
    }




    public double CenterY
    {
        get { return (double)GetValue(CenterYProperty); }
        set { SetValue(CenterYProperty, value); }
    }

    public static readonly DependencyProperty CenterYProperty =
        DependencyProperty.Register("CenterY", typeof(double), typeof(MyTestCircle),
        new UIPropertyMetadata(double.NaN,
            new PropertyChangedCallback(OnCenterYChanged),
            new CoerceValueCallback(OnCenterYCoerceValue)));

    public static void OnCenterYChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        MyTestCircle circle = (MyTestCircle)obj;
        Canvas.SetTop(circle, (double)args.NewValue - circle.Height / 2);
    }

    public static object OnCenterYCoerceValue(DependencyObject source, object obj)
    {
        MyTestCircle circle = (MyTestCircle)obj;
        return Canvas.GetTop(circle) + circle.Height / 2;
    }

}

Testing Canvas:

<Canvas x:Name="mainCanvas">
    <my:MyTestCircle Canvas.Left="104" Canvas.Top="132" x:Name="myTestCircle1" />
    <Line Stroke="Black"
        X1="0" Y1="0" 
        X2="{Binding ElementName=myTestCircle1, Path=CenterX}"
        Y2="{Binding ElementName=myTestCircle1, Path=CenterY}"
        />
</Canvas>

So, the Line in the Testing canvas does not follow the center of the circle… Why?

EDIT:

Remark:
In the code or Designer, I could never chanage the CenterX and CenterY properties. I need that that properties be “linked” with the Left/Top canvas properties… Is that possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:46:19+00:00Added an answer on May 17, 2026 at 1:46 am

    You are never actually setting CenterX and CenterY so they remain at their default but there are other issues. The question seems homeworkey so I’ll just point out the problems.

    Coerce handlers are used when you want to adjust a value before setting it e.g. limiting a value. In this situation they’re not needed plus you incorrectly cast obj which is a double to a MyTestCircle.

    Your change handlers are using the Width and Height of the MyTestCircle UserControl which haven’t been set, you should either set them or access size values that have been set. You could just have an Ellipse in the user control and set the size of the UserControl. You also have a typo calling the Y handler when X changes.

    If this is Homework try to fix the problems mentioned and we can have another look. If not and you just want a line to the Center I would remove the properties completely use the built-in Left,Top properties and add a RenderTransform that translates -50,-50.

    Do you need the extra properties? Do they need to update if you set Left and Top? If so custom Attached Properties would be a nice solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There was an interesting question in a practice test that I did not understand
How can I test my site in AOL without installing AOL? I did that
Did you ever had a bug in your code, you could not resolve? I
Did Microsoft abandon support for Windows Presentation Foundation (WPF) on Visual Studio 2005? There
Did anyone try that feature and has some feedback? Or Does anyone know some
Trying to create my own custom AttachedProperty for a WPF DependencyObject failed to actually
I have a Red Hat Linux box that I did not configure but must
I have a (custom) embedded jetty launcher which I had been using to develop/test
I have a custom EL function defined as follows: <function> ... <function-signature> java.lang.String getAsText(com.test.Outerclass.InnerEnum)
I did a test on a table (with innodb and myisam), which use PHP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.