1
votes

I have a CartPage in WindowsPhone App. It shows Items in cart with their selected quantities. I use a ListPicker to change the quantity. Now I have two questions,

  1. How to set the default Quantity in ListPicker (I am getting the Quantity of product from localstorage).
  2. When I select the quantity from ListPicker, How to save it into variable?

    <Grid.Resources>
    
        <DataTemplate x:Name="PickerFullModeItemTemplate">
            <StackPanel Orientation="Horizontal" Margin="16 21 0 20" >
                <TextBlock Name="TextQuantity" Text="{Binding Quantity}" Margin="16 0 0 0" FontSize="43" FontFamily="{StaticResource PhoneFontFamilyLight}"/>
    
            </StackPanel>
        </DataTemplate>
    </Grid.Resources>
    

<toolkit:ListPicker   toolkit:TiltEffect.IsTiltEnabled="True" Name="QuantityBox" Margin="264,91,142,36" Background="#FFA05E6A" FullModeItemTemplate="{StaticResource PickerFullModeItemTemplate}" BorderBrush="#FF8D7373" Foreground="#FF310836" FontSize="20" SelectionChanged="QuantityBox_SelectionChanged" MouseEnter="QuantityBox_MouseEnter" BorderThickness="1"/>

   public class ListQuantityClass
    {
        public int Quantity { get; set; }

    }

   List<ListQuantityClass> QuantitySource = new List<ListQuantityClass>();

                for (int i = 1; i <= 20; i++)
                {
                    QuantitySource.Add(new ListQuantityClass() { Quantity = i });
                }
                Custom.QuantityBox.ItemsSource = QuantitySource;

Both the below lines are giving me error:

                   Custom.QuantityBox.SelectedItem = cart.ProductQuantity;
                singletonInstance.QuantityChanged = int.Parse(QuantityBox.SelectedItem.ToString());

Actually Its obvious QuantityBox.SelectedItem WONT WORk because ListPicker is Databdound to QuantitySource list. What to use instead of QuantityBox.SelectedItem?

2
Can you please Look at this screenshot, I have marked my ListPicker. observe what it shows: How to get cart.ProductQuantity instead of XML_PARSING in it. s30.postimg.org/odk6tr4g1/Screenshot_24.pngAtìf Shabëër

2 Answers

1
votes

Since this is a two part question I will split it up into two sections.


  • How to set the default Quantity in ListPicker (I am getting the Quantity of product from localstorage).

You can set the SelectedItem only to an item in the ItemSource. Using your example,

public partial class MainPage : PhoneApplicationPage
{

    List<ListQuantityClass> QuantitySource = new List<ListQuantityClass>();

    // Constructor
    public MainPage()
    {
        InitializeComponent();

        for (int i = 1; i <= 20; i++)
        {
            QuantitySource.Add(new ListQuantityClass() { Quantity = i });
        }
        my_listpicker.ItemsSource = QuantitySource;

        // setting default value to 3
        my_listpicker.SelectedItem = QuantitySource[2];

        // fancy search and set
        // my_SetSelectedItemBasedOnQuantity(my_listpicker, 4); // this will set it to 4

    }
}

  • When I select the quantity from ListPicker, How to save it into variable?

This is a much more involved problem. You can loop through the Items in the ItemSource and check to see if it equals the SelectedItem and saved it that way. But I much prefer if you use an event, like so.

    private void my_listpicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        try
        {
            var listpicker = sender as ListPicker;
            if (listpicker != null)
            {
                var selected_item = listpicker.SelectedItem as ListQuantityClass;
                int quantity = selected_item.Quantity;

                // TODO: Save the value in quantity to a file or send it to a webservice
            }
        }
        catch (Exception ex)
        {
            string error_message = ex.Message;
        }

    }

XAML

    <Grid.Resources>
        <DataTemplate x:Name="PickerItemTemplate">
            <TextBlock Text="{Binding Quantity}" FontSize="43"/>
        </DataTemplate>

        <DataTemplate x:Name="PickerFullModeItemTemplate">
            <TextBlock Text="{Binding Quantity}" Margin="16 0 0 0" FontSize="43"/>
        </DataTemplate>
    </Grid.Resources>


    <toolkit:ListPicker Header="my list picker demo"  x:Name="my_listpicker" ItemTemplate="{StaticResource PickerItemTemplate}" FullModeItemTemplate="{StaticResource PickerFullModeItemTemplate}" SelectionChanged="my_listpicker_SelectionChanged" MaxHeight="300"/>

Function to set SelectedItem based on Quantity

    private void my_SetSelectedItemBasedOnQuantity(ListPicker lp, int quantity)
    {
        // first search for quantity if a match is found set it
        try
        {

            foreach (ListQuantityClass lqc in lp.ItemsSource)
            {
                // match found
                if (lqc.Quantity == quantity)
                {
                    lp.SelectedItem = lqc;
                }
            }
        }
        catch (Exception ex)
        {
            string error = ex.Message;
        }
    }
0
votes

I think you cannot use SelectedItem to set the selected item.You should set the SelectedIndex property instead. SelectedItem is used to get the selected item.Although the docs say that it can be used to set the selected item,I have never seen any practical implementation of the same.