UWP - Unable to Add ListBox Header (Not ListView)

Indudhar Gowda 426 Reputation points
2020-09-06T05:25:37.677+00:00

UWP - Unable to Add ListBox Header (Not ListView)

Simple ListBox Adding Header is Missing...

22833-1.png

Developer technologies | Universal Windows Platform (UWP)
Developer technologies | Windows Presentation Foundation
0 comments No comments
{count} votes

Answer accepted by question author
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2020-09-06T14:30:26.387+00:00

    Hi,
    for more then one column you can try this demo:

    <Page  
        x:Class="UWP10App1VB.Page19"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        xmlns:local="using:UWP10App1VB.App19"  
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
        mc:Ignorable="d"  
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
      <Page.DataContext>  
        <local:ViewModel/>  
      </Page.DataContext>  
      <Grid>  
        <ListBox ItemsSource="{Binding View}">  
          <ListBox.Template>  
            <ControlTemplate>  
              <StackPanel>  
                <Grid>  
                  <Grid.RowDefinitions>  
                    <RowDefinition Height="Auto"/>  
                    <RowDefinition Height="Auto"/>  
                  </Grid.RowDefinitions>  
                  <Grid.ColumnDefinitions>  
                    <ColumnDefinition Width="100"></ColumnDefinition>  
                    <ColumnDefinition Width="100"></ColumnDefinition>  
                  </Grid.ColumnDefinitions>  
                <TextBlock Grid.ColumnSpan="2" Text="Please select an item" FontWeight="Bold"/>  
                  <TextBlock Grid.Row="1" Grid.Column="0" Text="Title" HorizontalAlignment="Center"/>  
                  <TextBlock Grid.Row="1" Grid.Column="1" Text="Title 1" HorizontalAlignment="Center"/>  
                </Grid>  
                <ItemsPresenter></ItemsPresenter>  
              </StackPanel>  
            </ControlTemplate>  
          </ListBox.Template>  
          <ListBox.ItemTemplate>  
            <DataTemplate>  
              <Grid>  
                <Grid.ColumnDefinitions>  
                  <ColumnDefinition Width="100"></ColumnDefinition>  
                  <ColumnDefinition Width="100"></ColumnDefinition>  
                </Grid.ColumnDefinitions>  
                <TextBlock Text="{Binding Title}"/>  
                <TextBlock Grid.Column="1" Text="{Binding Title1}"/>  
              </Grid>  
            </DataTemplate>  
          </ListBox.ItemTemplate>  
        </ListBox>  
      </Grid>  
    </Page>  
    

    Result:

    22911-x.png

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2020-09-06T06:42:24.603+00:00

    Hi,
    you can use style like in following demo:

    XAML:

    <Page  
        x:Class="UWP10App1VB.Page19"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        xmlns:local="using:UWP10App1VB.App19"  
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
        mc:Ignorable="d"  
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
      <Page.DataContext>  
        <local:ViewModel/>  
      </Page.DataContext>  
      <Grid>  
        <ListBox ItemsSource="{Binding View}">  
          <ListBox.Template>  
            <ControlTemplate>  
              <StackPanel>  
                <TextBlock Text="Please select an item" FontWeight="Bold"/>  
                <ItemsPresenter></ItemsPresenter>  
              </StackPanel>  
            </ControlTemplate>  
          </ListBox.Template>  
          <ListBox.ItemTemplate>  
            <DataTemplate>  
              <TextBlock Text="{Binding Name}"/>  
            </DataTemplate>  
          </ListBox.ItemTemplate>  
        </ListBox>  
      </Grid>  
    </Page>  
    

    And Classes:

    Namespace App19  
      Public Class ViewModel  
        Public Sub New()  
          GetData()  
        End Sub  
      
        Public Property View As ObservableCollection(Of Data) = New ObservableCollection(Of Data)  
      
        Private Sub GetData()  
          For i = 1 To 10  
            View.Add(New Data With {.Name = $"Item {i}"})  
          Next  
        End Sub  
      
      End Class  
      
      Public Class Data  
        Public Property Name As String  
      End Class  
      
    End Namespace  
    

    Result:

    22827-x.png

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.