I need to implement something between listbox and panel. It has to have bindable itemssource without own style. So I decided to derived from FrameworkElement. I tried to add new button in code behind to my control and on changing ItemsSource property I call InvalidateMeasure. In MeasureOverride method I run over itemssource collection and call Measure with availableSize as parameter for each item. But as result I get weird DesiredSize of the control(in my case it was button) inside itemssouce - 16 height and 0 width. It's my code:
// part from usercontrol's code behind
public MainPage()
{
InitializeComponent();
dash.ItemsSource.Add(new Button { Content = "button 1 content", Padding = new Thickness(10) });
dash.ItemsSource.Add(new Button { Content = "button 2 content", Padding = new Thickness(10) });
}
//part from my control
protected override Size MeasureOverride(Size availableSize)
{
var desiredSize = new Size();
foreach (UIElement item in ItemsSource)
{
item.Measure(availableSize);
desiredSize.Height += item.DesiredSize.Height;
desiredSize.Width += item.DesiredSize.Width;
}
return desiredSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
double x = 25, y = 25;
foreach (FrameworkElement item in ItemsSource)
{
item.Arrange(new Rect(x, y, item.DesiredSize.Width, item.DesiredSize.Height));
}
return base.ArrangeOverride(finalSize);
}
I tried to set item Height and Width manually but it's not helped. Even tried to call Measure twice first time with new Size(double.PositiveInfinity,double.PositiveInfinity); And in any case buttons don't want to show up on user control.
Please help me to determine what i'm doing wrong!
ItemsPanelandItemContainerStyleorItemTemplateproperty of anItemsControlorListBox. Please tell us how the items shall be arranged, as currenlty you are trying to put them all on top of each other at x=25/y=25, which doesn't seem to make sense. - ClemensItemsSourceproperty could be any type of object. You should at least create a derived Panel, and add UIElements to itsChildrencollection. If you'd eleborate a bit on how you actually want to arrange the children, you may get a useful solution here. - ClemensFor scenarios that require application layout that is not possible using any of the predefined Panel elements, custom layout behaviors can be achieved by inheriting from Panel and overriding the default measure and arrange behavior by using the MeasureOverride and ArrangeOverride methods. - Clemens