0

I am working with two subclasses of FrameworkElement and trying to bind their widths and heights, so that when I set the Width or Height of one of the elements it updates the other element as well. Yet I'm setting something incorrectly with the bindings, as when I use Visual Studio's WPF Live Visual Tree to see the properties of the two FrameworkElements, I see that one has its Width and Height set correctly, while the other's is set to NAN. The code and snippets demonstrating the problem are below. What am I doing wrong?

Binding Source

Binding Source

Binding Target

Binding Target

Code to create binding

using System.Collections.Immutable;
using System.Windows;
using System.Windows.Data;
using Telerik.Windows.Controls.ChartView;

namespace CustomYAxis
{

    public class ChartYAxisAnnotation : CartesianCustomAnnotation
    {
        private readonly CustomYAxisView _annotationContent;

        public ChartYAxisAnnotation()
        {
            Content = _annotationContent = new CustomYAxisView();

            // These bindings aren't working
            var widthBinding = new Binding(nameof(WidthProperty))
            {
                Source = this,
            };

            var heightBinding = new Binding(nameof(HeightProperty))
            {
                Source = this
            };

            _annotationContent.SetBinding(WidthProperty, widthBinding);
            _annotationContent.SetBinding(HeightProperty, heightBinding);
        }
    }
}
5
  • just for debugging sake, have you tried adding a bogus converter just to see if you pass through it. Also have you tried setting Mode = TwoWay, just to see if anything changes Commented Sep 10, 2018 at 13:33
  • Also I would use WidthProperty.Name instead of nameof(WidthProperty) Commented Sep 10, 2018 at 13:41
  • Thanks for the suggestions! I just tried both things. The values do not pass through the bogus converter, and setting the Mode to TwoWay doesn't change anything. Commented Sep 10, 2018 at 13:41
  • Changing WidthProperty.Name does cause the values to pass through the bogus converter. But, the value is NAN. I'm editing my question to show how I'm setting the Width and Height on the first framework element. Commented Sep 10, 2018 at 13:44
  • Actually, changing the name to WidthProperty.Name was it! Thanks so much! If you want to add an answer I'll mark it as correct. Commented Sep 10, 2018 at 13:47

1 Answer 1

1

nameof(WidthProperty) actually returns "WidthProperty"

what you want to do is bind the property which name is "Width", same goes for HeightProperty

Therefore you should change nameof(WidthProperty) to WidthProperty.Name or "Width"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.