Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Two-way binding of the Text value of the text box and the ShapeNode.Bounds.Width value (Read 2352 times)
JackPan
Full Member
***
Offline


I Love MindFusion!

Posts: 134
Joined: Apr 9th, 2020
Two-way binding of the Text value of the text box and the ShapeNode.Bounds.Width value
Sep 21st, 2020 at 1:15am
Print Post  
Hi, there. Smiley

I try to implement the two-way binding of textBoxW and overlayNode.Bounds.Width and the two-way binding of textBoxH and overlayNode.Bounds.Height in the CreateNodes(string path) function in my program. The purpose is to change the Text value of textBoxW to change the width of the overlayNode or change the Text value of textBoxH to ​​change the height of the overlayNode. But my two lines of code failed to achieve this function, why?
Binding overlayNodeTextBoxWidth = new Binding() { Path = new PropertyPath("Bounds.Width"), Source = overlayNode };
            this.textBoxW.SetBinding(TextBox.TextProperty, overlayNodeTextBoxWidth);
            Binding overlayNodeTextBoxHeight = new Binding() { Path = new PropertyPath("Bounds.Height"), Source = overlayNode };
            this.textBoxH.SetBinding(TextBox.TextProperty, overlayNodeTextBoxHeight);

Any assistance would be appreciated.

Cheers,

Jack
  

9_21_0.png (Attachment deleted)
Back to top
 
IP Logged
 
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: Two-way binding of the Text value of the text box and the ShapeNode.Bounds.Width value
Reply #1 - Sep 21st, 2020 at 7:11am
Print Post  
Hi,

You can't bind to the Height/Width fields, but need to bind to the Bounds property instead. You will also need to implement a value converter to manage the Rect to string value transform.

Code
Select All
class RectToStringConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
    var bounds = (Rect)value;

    return parameter.ToString() == "W" ? bounds.Width.ToString("#.00") : bounds.Height.ToString("#.00");

  }

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  {
    throw new NotImplementedException();
  }
}

// ...

static RectToStringConverter converter = new RectToStringConverter();

// ...

var bindingWidth = new Binding("Bounds");
bindingWidth.Source = overlayNode;
bindingWidth.Mode = BindingMode.OneWay;
bindingWidth.Converter = converter;
bindingWidth.ConverterParameter = "W";

var bindingHeight = new Binding("Bounds");
bindingHeight.Source = overlayNode;
bindingHeight.Mode = BindingMode.OneWay;
bindingHeight.Converter = converter;
bindingHeight.ConverterParameter = "H";

textBoxW.SetBinding(TextBox.TextProperty, bindingWidth);
textBoxH.SetBinding(TextBox.TextProperty, bindingHeight); 



Regards,
Lyubo
MindFusion

  
Back to top
 
IP Logged
 
JackPan
Full Member
***
Offline


I Love MindFusion!

Posts: 134
Joined: Apr 9th, 2020
Re: Two-way binding of the Text value of the text box and the ShapeNode.Bounds.Width value
Reply #2 - Oct 19th, 2020 at 12:40pm
Print Post  
Thanks very much, Lyubo. Smiley

I got it.

Best regards.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint