I have an application where I used caliburn.micro and I observed some strange behavior. I have a grid with two colums, which are set to be equally broad. In this grid, there are two ListBoxes (see image below).
Now, if an item gets added to one ListBox and the other is empty, the ListBox with the item gets as broad as the item and the other one gets smaller, which means that the two ListBoxes aren't equally broad anymore.
Now comes the weird part of the story. If I resize the window once before adding the item, everything works as expected.
I've extracted this example from a larger application where I observed the same behavior with ItemControlls and TreeViews. In my research I rebuilt this sample application with pure WPF and everything works fine. So I think this problem must be connected to caliburn.micro.
This is the XAML of MainView:
<UserControl x:Class="WpfResizeErrorTest.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Background="#FF3D3A3A">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ListBox Name="LeftItems" Grid.Column="0" Grid.Row="0" Margin="0,0,5,0" />
<ListBox Name="RightItems" Grid.Column="1" Grid.Row="0" Margin="5,0,0,0"/>
<Button Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Name="AddItem" Height="30" Margin="5,5,5,5" Content="Add Item" />
</Grid>
</UserControl>
And the code of the corresponding view model:
public class MainViewModel : Screen
{
public MainViewModel()
{
this.LeftItems = new BindableCollection<string>();
this.RightItems = new BindableCollection<string>();
}
public BindableCollection<string> LeftItems { get; set; }
public BindableCollection<string> RightItems { get; set; }
public void AddItem()
{
this.LeftItems.Add("This is a long text which is usually longer than the list box it comes in.");
}
}
Does anyone know what the problem could be?
A colleague of mine has found the solution. Caliburn.micro changes the value of the
SizeToContent
property of theWindow
object. You have to do this in the bootstrapper:And everything works as expected.