Like Chris Said, we can add a property to the ViewModel to control the expanding behavior, but this is necessary to change the ViewModel. If you do not want to do this, here is another approach:
First, we need two DataTemplate , one to display brief information, and the other to display details. For instance:
<Page.Resources> <DataTemplate x:Key="ItemTemplate" x:DataType="src:Person"> <StackPanel Width="Auto" HorizontalAlignment="Stretch"> <TextBlock Margin="12, 15, 12, 0" FontSize="18.667" Text="{x:Bind Path=Name, Mode=OneWay}" /> <TextBox Margin="12, 12, 12, 0" HorizontalAlignment="Stretch" FontSize="18.667" Text="{x:Bind Path=Name, Mode=TwoWay}" /> </StackPanel> </DataTemplate> <DataTemplate x:Key="SelectedTemplate" x:DataType="src:Person"> <StackPanel Width="Auto" HorizontalAlignment="Stretch"> <TextBlock Margin="12, 15, 12, 0" FontSize="18.667" Text="{x:Bind Path=Name, Mode=OneWay}" /> <TextBox Margin="12, 12, 12, 0" HorizontalAlignment="Stretch" FontSize="18.667" Text="{x:Bind Path=Name, Mode=TwoWay}" /> <StackPanel> <TextBlock Margin="12, 15, 12, 0" Text="Date of birth" /> <DatePicker Margin="12, 5, 12, 0" Date="{x:Bind Path=DateOfBirth, Mode=TwoWay}" /> <TextBlock Margin="12, 15, 12, 0" Text="Domicile" /> <TextBox Margin="12, 5, 12, 0" Text="{x:Bind Path=Domicile, Mode=OneWay}" /> </StackPanel> </StackPanel> </DataTemplate> </Page.Resources>
Then, in the ListBox set the default ItemTemplate to compose the information template.
<ListBox ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{x:Bind Persons}" SelectionChanged="ListBox_SelectionChanged"> <ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Setter Property="HorizontalContentAlignment" Value="Stretch" /> </Style> </ListBox.ItemContainerStyle> </ListBox>
Finally, add an event handler to the SelectionChanged event, and in this change to the ContentTemplate handler for the selected and unselected item.
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { var listBox = sender as ListBox; //get unselected item var unselectedPerson = e.RemovedItems.FirstOrDefault() as Person; if (unselectedPerson != null) { //get unselected item container var unselectedItem = listBox.ContainerFromItem(unselectedPerson) as ListBoxItem; //set ContentTemplate unselectedItem.ContentTemplate = (DataTemplate)this.Resources["ItemTemplate"]; } //get selected item container var selectedItem = listBox.ContainerFromItem(listBox.SelectedItem) as ListBoxItem; selectedItem.ContentTemplate = (DataTemplate)this.Resources["SelectedTemplate"]; }