question

IDGO-6443 avatar image
0 Votes"
IDGO-6443 asked ·

UWP - Unable to Add ListBox Header (Not ListView)

UWP - Unable to Add ListBox Header (Not ListView)

Simple ListBox Adding Header is Missing...

22833-1.png


windows-uwpwindows-wpfwindows-uwp-xaml
1.png (10.6 KiB)
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PeterFleischer-3316 avatar image
1 Vote"
PeterFleischer-3316 answered ·

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



x.png (11.5 KiB)
· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PeterFleischer-3316 avatar image
1 Vote"
PeterFleischer-3316 answered ·

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



x.png (8.5 KiB)
· 2 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hey Peter, for 1 Column Header is Fine ...I have 2 Columns, So I need 2 Column Header

0 Votes 0 · ·

I tried below Code, but not working appropriately...
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="" />
<ColumnDefinition Width="
" />
</Grid.ColumnDefinitions>
<TextBlock Text="Test"/>
<TextBlock Grid.Column="1" Text="Test1"/>
<ItemsPresenter Grid.Column="0" ></ItemsPresenter>
</Grid>
</ControlTemplate>
</ListBox.Template>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="" />
<ColumnDefinition Width="
" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Title}"/>
<TextBlock Grid.Column="1" Text="{Binding Title1}"/>
22830-2.png


0 Votes 0 · ·
2.png (4.6 KiB)
IDGO-6443 avatar image
0 Votes"
IDGO-6443 answered ·

Thanks Peter

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.