listview - WPF: How to make two controls (list views) share whole available space between them? -
i have 2 list views (vertically aligned) might have various number of items. i'd them share space proportionally when needed (this can achieve regular grid , *) when 1 list view doesn't have many items show other list view fill whole space. , vice versa.
tried different things not achieve behavior.
for instance grid can specify * , * (or other proportions) means half of space empty if 1 of list views not have items (and has tons of them).
is there way achieve this? need implement own panel or there (simpler) way it?
thank you! zaki
ok, try code:
class mypanel : panel { protected override size measureoverride(size constraint) { // first measuring desired size of children var availablesize = new size(constraint.width, double.positiveinfinity); foreach (uielement ui in internalchildren) ui.measure(availablesize); var totalheight = internalchildren.oftype<uielement>().sum(x => x.desiredsize.height); // resizing children within constraint var factor = (totalheight == 0 ? 1.0 : constraint.height / totalheight); foreach (uielement ui in internalchildren) ui.measure(new size(constraint.width, ui.desiredsize.height * factor)); var maxwidth = internalchildren.oftype<uielement>().max(x => x.desiredsize.width); return new size(math.min(constraint.width, maxwidth), math.min(constraint.height, totalheight)); } protected override size arrangeoverride(size arrangesize) { // aligning children vertically var totalheight = internalchildren.oftype<uielement>().sum(ui => ui.desiredsize.height); var y = 0.0; var rect = new rect(arrangesize); foreach (uielement ui in internalchildren) { rect.y += y; y = ui.desiredsize.height; rect.height = y; ui.arrange(rect); } return arrangesize; } }
this panel arrange children vertically , give children vertical space proportionally desired height, won't allow them take more space available.
so, if, example, have 200px height available, first list view wants 150px, , second wants 100px, scaled down 120px + 80px == 200px
Comments
Post a Comment