I am creating a collapsible panel element, which would essentially be a panel element with a button element and a panel element below the button. Clicking the button causes the neighboring panel to have Visible = false. I would like to resize the containing panel when the child panel is set to invisible.
I have done this manually, by setting the Size property to be the sum of the widths and heights of the visible elements (either the button or the button and the child panel.)
I am curious to know though if there was a way to force the resize of the containing panel without manually calling Size.
I guess I'm looking for the inverse of the property Dock=Fill, which automatically resizes elements based on the size of their containing element.
Thanks in advance.
How about doing:
panel1.Size = new Size(0, 0);
panel1.AutoSize = true;
and then instead of changing the visibility, do this:
panel1.Controls.Remove(panel2);
and when you want to bring it back:
panel1.Controls.Add(panel2);
(panel1 is the back-panel)
I just tried the answer given by ispiro. You dont need to remove and add the control. Setting Visible can work. It depends on when you perform the layout. If panel2 performs the layout before panel1, panel2 will not resize. To make it easier, use the parents PerformLayout instead.
It's used like this:
panel1.ResumeLayout(false);
panel2.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
Related
In window forms, i created a panel dynamically, in that i want to place labels which are dynamically created, now i want to place the lables in a neat format, i mean alignment,line by line. Every time i am creating the label and binding the label to panel until the data is there in data set. so i want proper arrange for that label, is there any reference, could be very appreciate, thank you.
You could try to use the FlowLayoutPanel instead of the ordinary Panel.
Set it to order in the direction you want, optionally turn of wrapping, and you would have the content organized automatically.
Optionally you would need to traverse your panel's labels and set (pseudo, vertical direction):
currentLabel.Top = prevLabel.Top + prevLabel.height + mySpacing
I have grid with some textboxes and an image which goes out of grid boundaries and I add this grids dynamically in my code to another grid.
I want to have my image on top of all the rest of the grids. Unfortunately each grid I add to the root grid will hover over my previous grid image.
Could you please suggest anything?! I have tried with zindex and it did not work in my case.
What I am doing wrong?
ZIndex only works for the immediate children of a panel-derived container (Grid, Canvas, Stackpanel etc). Otherwise it is down to the order they exist in the visual tree (i.e. the last one gets displayed on top).
If you have nested objects you simply need to think about the order they are added. The simplest way to do this is have two top level grids/panels, the first contains everything else and the last containing just the dynamically added children.
This way whatever you put in the second grid will always be on top of all other items (in the first grid/container).
It gets basicaly to this:
I have a canvas container (x:name="Container" - to reffer later to), with some other controls(e.g. I have a Button 30x60 in size) and I want to scale the Container to fit the button, without having to resize anything. (just using scaleTransform)
If I set a slider binded to the Container Scale, I can slide it to fit the button, but the value is 1.1343 and I want to be able somehow to compute this value programatically, because all the controls inside Container are of different sizes, and resizable in adition.
The question is: is there a way to scale the Container so that a Control from the container is displayed on the entire Container surface? Also the position will have to be adjusted, so that the scrolling vierew of the Container should be positioned over the control, but that is done already.
Thank you in advance,
Daniel
Use Grid as the container. Any control placed inside of the Grid will directly fill the whole Grid, and its size (the width and height) will match the size of the Grid.
It seems when you add a user control using a grid as a container, the usercontrol, which comprises a set of labels and an array of buttons will not align to the top but arranged somewhere in the middle.
I have stack panel with custom controls in it. User can add or remove the items.
I have attached MouseDragElementBehavior to each item. So now user can move them within the stack panel.
However the items now are arranged on arbitrary manner. Is a mess really. They stay where the user left them.
What I need now is to make them to be stacked as the stack panel supposed to be... Nicely one by one...
So I need to simply let user change the order of items by using drag / drop operation but items has to be stacked precisely.
There is DragFinished event, but I dont really see how the Behavior moves items. I thought it is Margin it changes but margins stays 0... I dont know what to do next.
Appreciate little help.
MouseDragElementBehavior does its work using a Transform on the RenderTransform property of the attached element (i.e. the one the behavior is applied to) (the exact type of transform depends on the state of the RenderTransform property but it will either be a TranslateTransform or a MatrixTransform).
The transform is not reset when you finish dragging (nor would you want it to be because the element would snap back to its position in the StackPanel when you started dragging) and this is why the element stays where "the user left them".
To change the items position in the StackPanel in the way you are talking about you will have to use the DragFinished event. What you probably want to do is work out where in the StackPanel the element will end up (as a result of the drag, i.e. which element in the panel it will "push" down) then create an animation to animate the element from its current position (where the user released the drag) to that final position in the StackPanel and another animation to move the "pushed" element in to its new position in the StackPanel (VisualTreeHelper can probably help here (I think)). Once those animations are finished just set the new index within the StackPanel for each item and remove the RenderTransform translation.
I have a FormPanel that includes a bunch of TextField, I also add more to it dynamically using FormPanel.add(), FormPanel.doLayout(), however the height doesn't readjust itself, so extra textfields are not visible. what's going on?
thanks
Either the FormPanel (and all higher level containers) have to be auto height (autoHeight: true) so that the browser will handle sizing, or else you will need to recalculate the height after adding and set it programmatically. I've done it both ways, depending on the layout.