【发布时间】:2014-07-30 05:01:02
【问题描述】:
我在页面中有一个自定义用户控件。在此自定义用户控件中有标签、删除项目按钮和数量文本框。 在我的页面中有一个 ListBox 从列表中获取项目,列表是从本地存储生成的。
现在,当我单击删除项目按钮时,它会转到 ListBox_SelectionChanged 并获取 Selectedindex 并进一步转到 DeleteButton_Tap 并为其提供 Selectedindex 并删除该项目。
现在的问题是,如果我在 Quantity 文本框内单击,它不会首先转到 ListBox_SelectionChanged 来获取要更新数量的项目的 SelectedIndex。但它直接转到自定义用户控件中的 QuantityBox_TextChanged。
如何发送我要更改数量的项目的 SelectedIndex?
EDIT-----------------自定义控件的代码//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
namespace XMLParsing
{
public partial class CustomCartFavControl : UserControl
{
public CustomCartFavControl()
{
InitializeComponent();
}
Singleton singletonInstance = Singleton.mft;
private const string strConnectionString = @"isostore:/MFTDB9.sdf";
private void deleteItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
//Confirm:
MessageBoxResult resultDelete = MessageBox.Show("Delete This Item From Cart?", "Cart:", MessageBoxButton.OKCancel);
if (resultDelete == MessageBoxResult.OK)
{
using (MFTDataContext MFTdb = new MFTDataContext(strConnectionString))
{
IQueryable<Database> ListQuery = from Item in MFTdb.MFTCart where Item.ProductUniqueID == singletonInstance.CartItemIDs[singletonInstance.ItemToChange] select Item;
Database itemRemove = ListQuery.FirstOrDefault();
MFTdb.MFTCart.DeleteOnSubmit(itemRemove);
MFTdb.SubmitChanges();
}
singletonInstance.somethingDeletedFromCart = true;
}
else if (resultDelete == MessageBoxResult.Cancel)
{
singletonInstance.somethingDeletedFromCart = false;
}
//END
}
private void QuantityBoxLabel_TextChanged_1(object sender, TextChangedEventArgs e)
{
if (QuantityBoxLabel.Text != "")
{
singletonInstance.QuantityChanged = int.Parse(QuantityBoxLabel.Text);
}
}
private void QuantityBoxLabel_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
//Update Cart
if (singletonInstance.QuantityOriginal == singletonInstance.QuantityChanged)
{
//Do Nothing! Quantity Not Changed
singletonInstance.SomethingChanged = false;
}
else
{
//Work here for Quantity Update etc code:
singletonInstance.SomethingChanged = true;
//((Cart)this.Page).UpdateCartMethod();
if (singletonInstance.QuantityChanged == 0)
{
// new quantity =0
MessageBox.Show("You Didn't Enter A Valid Quantity. Quantity Changed Back To: " + singletonInstance.QuantityOriginal);
QuantityBoxLabel.Focus();
}
else
{
if (singletonInstance.UpdatedSuccessfully == false)
{
// new quantity !=0
//Test on Product ID:519
// Chnange to dynamic via SlectionIndex or anything..
//DISABLE FOR TESTING:
//using (MFTDataContext MFTdb = new MFTDataContext(strConnectionString))
//{
// var itemToChange = (from item in MFTdb.MFTCart
// where item.ProductID == singletonInstance.ItemToChange
// select item).Single();
// itemToChange.ProductQuantity = singletonInstance.QuantityChanged;
// itemToChange.ProductTotalPrice = singletonInstance.QuantityChanged * itemToChange.ProductPrice;
// MFTdb.SubmitChanges();
//}
singletonInstance.SomethingChanged = false;
singletonInstance.UpdatedSuccessfully = true;
}
else
{
}
(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/Cart.xaml?refersh=" + DateTime.Now.ToString(), UriKind.Relative));
}
}
}
private void QuantityBoxLabel_LostFocus(object sender, RoutedEventArgs e)
{
// Do same as QuantityBoxLabel_MouseLeave
QuantityBoxLabel_MouseLeave(null, null);
}
private void QuantityBoxLabel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
//Cant be 0 . Already Filtered in ThirdPage
if (QuantityBoxLabel.Text != "")
{
singletonInstance.QuantityOriginal = int.Parse(QuantityBoxLabel.Text);
}
}
private void QuantityBoxLabel_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
// TESTING;
// this.QuantityBoxLabel.IsHitTestVisible = false;
//Cant be 0 . Already Filtered in ThirdPage
singletonInstance.QuantityOriginal = int.Parse(QuantityBoxLabel.Text);
}
private void QuantityBoxLabel_LayoutUpdated(object sender, EventArgs e)
{
QuantityBoxLabel.Focus();
}
}
}
【问题讨论】:
-
正如你所说,数量文本框似乎在自定义控件中,点击数量文本框只会带你到 Text_Changed。它应该在 ListBox 中才能进入 Selection_Changed 对吗?你的 ListBox 中有那个 CustomControl 吗?
-
我在 CustomControl 中有删除按钮和数量文本框。当我单击 utton 时,它首先将我完美地带到 ListBox_SelectionChanged。但是为什么不点击里面数量文本框带我首先到 ListBox_SelectionChanged ?
-
您可以添加数量对吗?还是您从本地存储中检索该值?
-
请检查链接是否已发布。它是在列表框中生成的我的 CutmControl。此外,它在文本框中显示所选项目的数量。我想更改数量,在 MouseLeave、LostFocus 方法/事件处理程序上,我编写了更新数量的代码,但它需要 productid 或列表框选择索引,以便它了解我们正在更改的项目数量。只是在删除按钮的情况下,它首先转到 listbox_selectionchanged,为什么不先单击文本框转到 listbox_selectionchanged。我现在如何将选定的索引转发到更新方法:(
标签: c# xaml windows-phone-8 shopping-cart