TomaszRewak / C-sharp-console-gui-framework

A GUI framework for C# console applications
MIT License
1.08k stars 45 forks source link

Custom control margin not updating. #30

Closed JesFo closed 1 year ago

JesFo commented 1 year ago

I have a custom control for labels with an equal spacing using the Margin control:

    internal class LabelControl: SimpleControl
    {
        private readonly TextBlock _label;
        private readonly TextBlock _value;
        private Offset _offset;
        public LabelControl()
        {
            _label = new TextBlock();   //┤
            _value = new TextBlock();
            _offset = new Offset(0,0,0,0);
            Content = new Margin
            {
                Offset = _offset,
                Content = new Overlay
                {

                    TopContent = _label,
                    BottomContent = _value
                }
            };
        }
        public Offset Margin
        {
            get => _offset;
            set => _offset = value;
        }
        public string Label
        {
            get => _label.Text;
            set => _label.Text = value;
        }
        public string Value
        {
            get => _value.Text;
            set => _value.Text = value;
        }
    }

Changing parameters Labeland Valuewithin each instance after initialization changes the text within the accordingly assigned TextBlock controls just fine (e.g, mylabelcontrol.Value = "new value", mylabelcontrol.Label = "new label"). However, assigning a new Offset for the Margin control after initialization yields no result (e.g: mylabelcontrol.Margin = new Offset(1,2,3,4)). Is this behaviour by design?

TomaszRewak commented 1 year ago

Hey Jesse!

It seems that you are assigning the new offset to a local field of the LabelControl and not the Margin control within it.

You might want to change your code to be like this:

    internal class LabelControl: SimpleControl
    {
        private readonly TextBlock _label;
        private readonly TextBlock _value;
        private readonly Margin _margin;

        public LabelControl()
        {
            _label = new TextBlock();   //┤
            _value = new TextBlock();
            _margin = new Margin
            {
                Offset = new Offset(0,0,0,0),
                Content = new Overlay
                {

                    TopContent = _label,
                    BottomContent = _value
                }
            };

            Content = _margin;
        }
        public Offset Margin
        {
            get => _margin.Offset;
            set => _margin.Offset = value;
        }
        public string Label
        {
            get => _label.Text;
            set => _label.Text = value;
        }
        public string Value
        {
            get => _value.Text;
            set => _value.Text = value;
        }
    }

This way you "bind" the Offset of the internal Margin control to the Offset of your LabelControl.

Please let me know if that worked, as I did not run/test it.

JesFo commented 1 year ago

Hey Jesse!

It seems that you are assigning the new offset to a local field of the LabelControl and not the Margin control within it.

You might want to change your code to be like this:

    internal class LabelControl: SimpleControl
    {
        private readonly TextBlock _label;
        private readonly TextBlock _value;
        private readonly Margin _margin;

        public LabelControl()
        {
            _label = new TextBlock();   //┤
            _value = new TextBlock();
            _margin = new Margin
            {
                Offset = new Offset(0,0,0,0),
                Content = new Overlay
                {

                    TopContent = _label,
                    BottomContent = _value
                }
            };

            Content = _margin;
        }
        public Offset Margin
        {
            get => _margin.Offset;
            set => _margin.Offset = value;
        }
        public string Label
        {
            get => _label.Text;
            set => _label.Text = value;
        }
        public string Value
        {
            get => _value.Text;
            set => _value.Text = value;
        }
    }

This way you "bind" the Offset of the internal Margin control to the Offset of your LabelControl.

Please let me know if that worked, as I did not run/test it.

This worked fine. Thank you for your quick response.