2

Does anyone know of the correct way to add multiple sections of text to one listview item when using Windows 10 universal apps in C#? Items.subitems doesn't seem to work. Say my two string I want to display in a single list item are stored in...

    public class listContent
    {
        public string heading { get; set; }
        public string subHeading { get; set; }
    }

Would doing something similar to this work?

listContent listItem = new listContent();
listItem.heading = "HEADING";
listItem.subHeading = "subheading";
lsvTransactions.Items.Add(listItem);

Regards Nathan

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Xylynx
  • 267
  • 1
  • 3
  • 11

3 Answers3

2

As it was said maybe if you give us more info on your code, we can help better. I post you here a sample of a Listbox (which is similar to the listview) with the definition of the ItemTemplate for the elements I use and how to bind the items to the listbox and the fields in the items to the elements put in the Listbox.

    <ListBox Grid.Row="0" Grid.Column="0" ItemsSource="{Binding Path=MeasureUnits, Mode=OneWay}"
              SelectedItem="{Binding Path=SelectedMeasureUnit, Mode=TwoWay}"
            SelectionMode="Single" Margin="0" 
              IsSynchronizedWithCurrentItem="True" >
        <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <WrapPanel>
                            <TextBlock Text="{Binding Path=ID}" Style="{StaticResource idStyle}" />
                            <TextBlock Text="{Binding Path=Description}" Style="{StaticResource desStyle}"/>
                        </WrapPanel>
                        <WrapPanel>
                            <TextBlock Text="{Binding Path=DestinationUnitsInSource}" Style="{StaticResource numStyle}" />
                            <TextBlock Text="{Binding Path=SourceUnitsInDestination}" Style="{StaticResource numStyle}" />
                        </WrapPanel>
                    </StackPanel>
                </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
Sabrina_cs
  • 421
  • 3
  • 18
  • 1
    Wrap Panel is not supported in Universal and IsSynchronizedWithCurrentItem is not supported, but I upvote this :) – Seth Kitchen Sep 15 '15 at 18:32
  • 1
    You can simply remove all the unsupported code here. I don't think he wants a `WrapPanel` in his case, a `StackPanel` would do. – Justin XL Sep 15 '15 at 21:51
2

Best way is to DataBind to a custom DataTemplate. Considering you already have lsvTransactions name ListView. Here is what you should do.

  1. Create a List of listContent in your .cs file showed below

    public class MainPage : Page
    {
        public List<listContent> ContentList {get; set;}
        .....
    
  2. Add Items to this ContentList in your code like you did above.

  3. After adding items add the following line to point to data source.

    lsvTransactions.DataContext = ContentList;
    
  4. Create a ListView like shown below with DataTemplate

    <ListView x:Name="lsvTransactions" ItemsSource={Binding}>
            <ListView.ItemTemplate>
                    <DataTemplate>
                            <StackPanel>
                                    <TextBlock Text="{Binding heading}"/>
                                    <TextBlock Text="{Binding subheading}"/>
                            </StackPanel>
                    </DataTemplate>
            </ListView.ItemTemplate>
    </ListView>
    
Gaurav Chouhan
  • 265
  • 2
  • 13
  • `` this line returns an error on "ItemsSource={Binding}. When I change it to "ItemsSource="{Binding}" the error goes but nothing appears in the list box – Xylynx Sep 17 '15 at 15:31
0

In my XAML:

 <ListView x:Name="listView">

In my C#:

listView.Items.Add(item);

Where Item is an object with multiple text elements.

If you post code I can be more specific.

Seth Kitchen
  • 1,526
  • 19
  • 53
  • Even though if you add it to listView like this, your `ListView` will have no idea how to show these these two elements :) You will need to define a DataTemplate to make it possible. – Gaurav Chouhan Sep 16 '15 at 03:29