我在WindowsPhone App中有一个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 WONT WORk因为ListPicker是Databdound到QuantitySource列表。使用什么而不是QuantityBox.SelectedItem?
答案 0 :(得分:1)
由于这是一个两部分问题,我将把它分成两部分。
您只能将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;
}
}
答案 1 :(得分:0)
我认为您无法使用SelectedItem
来设置所选项目。您应该设置SelectedIndex
属性。 SelectedItem
用于获取所选项目。尽管文档说它可用于设置所选项目,但我从未见过相同的实际实现。