1

I have a listview containing some item. And I want to expand the item to show detail information when I select one item, what should I do?

THaGKI9
  • 86
  • 4
  • Welcome to Stack Overflow! Please review our [SO Question Checklist](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) to help you to ask a good question, and thus get a good answer. – Joe C Oct 13 '16 at 17:15
  • There is no Default Control that does this. However there are CustomControls for similar Behaviour. Take a look at [this](http://deanchalk.com/a-xaml-uwp-custom-control-the-expander/) – AVK Oct 13 '16 at 17:36
  • @AVKNaidu, It's very kind of you to provide such a CustomControl, but I want to say with the default `ListView` control, this work can be done, you can check my answer for details. – Grace Feng Oct 14 '16 at 05:15
  • @AVKNaidu your solution is amazing but I prefer raw ListView control. Thank you~ – THaGKI9 Oct 17 '16 at 03:26

1 Answers1

3

I didn't check the CustomControl carefully which provided by @AVK Naidu, which is good and seems can solve your problem. But I need to say here, it is totally possible to do this work with the default ListView control, what you need is just changing the DataTemplate for your ListViewItem when it is selected.

Just for example here:

<Page.Resources>
    <DataTemplate x:Name="Normal" x:Key="Normal">
        <TextBlock Text="{Binding Name}" />
    </DataTemplate>
    <DataTemplate x:Name="Detail" x:Key="Detail">
        <StackPanel>
            <TextBlock Text="{Binding Name}" FontSize="30" Foreground="Red" HorizontalAlignment="Center" />
            <TextBlock Text="Details:" FontSize="30" Foreground="Blue" Margin="0,10" />
            <TextBlock Text="{Binding Details}" FontSize="20" />
        </StackPanel>
    </DataTemplate>
</Page.Resources>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ListView ItemTemplate="{StaticResource Normal}"
              ItemsSource="{x:Bind Collection}" SelectionChanged="listView_SelectionChanged" />
</Grid>

Code behind for listView_SelectionChanged:

private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    //Assign DataTemplate for selected items
    foreach (var item in e.AddedItems)
    {
        ListViewItem lvi = (sender as ListView).ContainerFromItem(item) as ListViewItem;
        lvi.ContentTemplate = (DataTemplate)this.Resources["Detail"];
    }
    //Remove DataTemplate for unselected items
    foreach (var item in e.RemovedItems)
    {
        ListViewItem lvi = (sender as ListView).ContainerFromItem(item) as ListViewItem;
        lvi.ContentTemplate = (DataTemplate)this.Resources["Normal"];
    }
}

Result:

enter image description here

Grace Feng
  • 16,564
  • 2
  • 22
  • 45
  • Wow! This is absolute what I want! Thank you^_^ – THaGKI9 Oct 17 '16 at 03:27
  • But I feel it's not property to assign different size to different item, cuz this will influence user experience in some way. So finally I add a detail page for my app. – THaGKI9 Oct 17 '16 at 09:41
  • @THaGKI9, `But I feel it's not property to assign different size to different item`, you mean not properly? I think it bases on what you need to achieve in your app. And implementation a master/detail is also a method, to implement this, you can refer to the official [Master/detail sample](https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/XamlMasterDetail). – Grace Feng Oct 17 '16 at 09:54