0

Hi I am trying to add items to a listview but get the following error, 'ListViewItem' does not contain a constructor that takes 1 argument. I am getting the error here new ListViewItem(row); I am using Windows phone 8.1 in c#.

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        int totalq = int.Parse(textBox.Text) + app.vanilla;
        textBox.Text = totalq.ToString();
        double totalvanilla = Convert.ToDouble(totalq)* 1.50;
        textBox_Copy.Text = totalvanilla.ToString();
        //listBox.Items.Add(totalq.ToString() + "Vanilla");

        string[] row = { totalq.ToString(),"vanilla" };
        var listViewItem = new ListViewItem(row); 

        listView.Items.Add(listViewItem);


    }
samuel
  • 31
  • 4

3 Answers3

3

I think you're the System.Windows.Forms.ListViewItem (which has a constructor for taking a string array) with the xaml-based System.Windows.Controls.ListViewItem.

You'd use the latter in another way:

XAML:

<ListView x:Name="listView">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}"/>
            <GridViewColumn Header="Qunatity" DisplayMemberBinding="{Binding Path=Quantity}"/>
        </GridView>
    </ListView.View>
</ListView>

Code:

int totalq = 23;
var row = new { Quantity = totalq, Name = "vanilla" };

listView.Items.Add(row);

Edit:

If GridView isn't an option for you and you want to / have to put everything in place yourself, you can use following approach (based on this answer):

<ListView x:Name="listView">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid VerticalAlignment="Center">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Column="0" Text="{Binding Quantity}" />
                <TextBlock Grid.Column="1" Text="{Binding Name}" />
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
Community
  • 1
  • 1
Jan Köhler
  • 5,817
  • 5
  • 26
  • 35
  • Gridview column does not exist in windows phone 8.1. It shows an error when I use this method. – samuel Apr 17 '16 at 17:18
0

You are sending string as parameters to listviewtem.ListViewItem has content property ,

Do like this

listView.Items.Add(new ListViewItem{Content = whateveryourstring});
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

I think you need to add a class with the property that you want to show at view. Like this.

public class ListModel {
    public string TotalQuantity { set; get; }
    public string Name { set; get; }
}

Then try this XAML

xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"

<toolkit:ListView Name="listView">
    <toolkit:ListView.ItemTemplate>
       <DataTemplate>
           <Grid>
               <Grid.ColumnDefinitions>
                   <ColumnDefinition Width="100" />
                   <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>                       
                <TextBlock  Text="{Binding TotalQuantity}" />
                <TextBlock Grid.Column="1" Text="{Binding Name}" />
           </Grid>
       </DataTemplate>
   </toolkit:ListView.ItemTemplate>
</toolkit:ListView>

Finaly add the itemsource for list view

int totalq = int.Parse("3") + 2;
textBox.Text = totalq.ToString();
double totalvanilla = Convert.ToDouble(totalq) * 1.50;
textBox_Copy.Text = totalvanilla.ToString();
listBox.Items.Add(totalq.ToString() + "Vanilla");

ListViewItems = new ObservableCollection<ListModel>();
ListViewItems.Add(new ListModel()
{
    TotalQuantity = totalq.ToString(),
     Name = "vanilla"
});
listView.ItemsSource = ListViewItems;
reza.cse08
  • 5,938
  • 48
  • 39