我在WindowsPhone应用程序中有一个CartPage。它显示了购物车中的物品及其所选数量。我使用ListPicker来改变数量。现在我有两个问题,
- 如何在ListPicker中设置默认数量(我从本地存储中获得产品数量)。
-
当我从列表选择器中选择数量时,如何将其保存为变量?
<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;
下面两行都给我错误:
Custom.QuantityBox.SelectedItem = cart.ProductQuantity;
singletonInstance.QuantityChanged = int.Parse(QuantityBox.SelectedItem.ToString());
实际上它是明显的QuantityBox。SelectedItem不会工作,因为ListPicker是数据库绑定到定量资源列表。用什么代替QuantityBox.SelectedItem?
由于这是一个由两部分组成的问题,我将把它分成两个部分。
- 如何在列表选择器中设置默认数量(我正在获得本地存储的产品数量
您只能将SelectedItem设置为ItemSource中的一个项。使用您的示例,
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
}
}
- 当我从列表选择器中选择数量时,如何将其保存到变量?
这是一个更复杂的问题。您可以遍历ItemSource中的Items,检查它是否等于SelectedItem,并以这种方式保存它。但我更喜欢你用事件,像这样。
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"/>
基于数量设置SelectedItem的功能
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;
}
}
我认为您不能使用SelectedItem
来设置所选项目。您应该设置SelectedIndex
属性。SelectedItem
用于获取所选项。虽然文档说它可以用来设置选中的项目,但我从未见过任何实际的实现。