I was creating a small WPF application and found an interesting thing while playing around with some properties and events. Let me first explain what the application does. (I have also attached the code for more details)
It’s a simple application which lists down all the contacts from a text file and displays its details like first name, last name, email etc. I am using a Contact class to hold all the contact details and I am using a PageFunction to display the contact details on the main window. I have added a few more fancy things to it but that’s not relevant to this context.
The Main Window of the application has a ListBox on the left DockPanel which is bound with the contacts.txt file using ObjectDataProvider. The right DockPanel displays a PageFunction on the ListItemSelected event of the ListBox, which displays the contact details.

Initially the Main Window had the following xaml code:
<Window x:Class="AddressBook.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="AddressBook"
Loaded="WindowLoaded"
SizeToContent="WidthAndHeight"
MinWidth="640"
MinHeight="480"
>
...
The WindowLoaded eventhandler contains the code to set the DataContext property of the left DockPanel to the contacts data.
private void WindowLoaded(object sender, RoutedEventArgs e)
{
DockPanel_LeftPane.DataContext = Application.Current.Properties["ContactList"];
}
This code was working fine and I was getting the contact details of the first item when I started the application. Then I tried changing the width and height of the window to MinWidth="1200" and MinHeight="800" and to my surprise the application stopped showing the contact details on the startup. I was only able to view the contact details once I clicked on the ListBox item.
I was playing with the values and when I changed it to MinWidth="1079" and MinHeight="800" it started working again!!!
I thought it might be something to do with the SizeToContent so I tried removing that but it again stopped working after removing it. I also tried various other things but it didn’t worked.
So finally to make the code work with 1200 * 800 I added the SizeChanged eventhandler and wrote the same code as in the WindowLoaded eventhandler.
<Window x:Class="AddressBook.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="AddressBook"
Loaded="WindowLoaded"
SizeChanged="WindowSizeChanged"
SizeToContent="WidthAndHeight"
MinWidth="1200"
MinHeight="800"
>
I have no idea what the problem was? Why does the size of the window affect the loading of the data ? Is the SizeChanged event fired every time after the Loaded event and it just unbinds everything done in the Loaded event ? This can’t be possible but this is what’s happening!!
While I am still trying to find the problem, I thought to share this in case anyone can find something useful and make this understandable to me.
All inputs are welcomed.
-Jomit