简体   繁体   English

Windows Phone中的自定义用户控件

[英]Custom User Control In Windows Phone

I have a Custom User Control in the page. 我在页面中有一个自定义用户控件。 In this Custom User Control there are labels, DELETE ITEM Button and Quantity Textbox. 在此“自定义用户控件”中,有标签,“删除项目”按钮和“数量”文本框。 In my page there is a ListBox which gets items from list, list is generated from local storage. 在我的页面中有一个ListBox,它从列表中获取项目,列表是从本地存储生成的。

Now When I click on DELETE ITEM Button, It Goes to ListBox_SelectionChanged and takes the Selectedindex and further goes to DeleteButton_Tap and provides Selectedindex to it and deletes the item. 现在,当我单击DELETE ITEM Button时,它将转到ListBox_SelectionChanged并获取SelectedIndex,然后转到DeleteButton_Tap并为其提供SelectedIndex并删除该项目。

Now the problem is, if i click inside Quantity Textbox, it does not first goto ListBox_SelectionChanged to get SelectedIndex of item you want to update the quantity. 现在的问题是,如果我在“数量”文本框内单击,则不会首先转到ListBox_SelectionChanged以获取要更新数量的项目的SelectedIndex。 but it directly goes to QuantityBox_TextChanged inside Custom User Control. 但它直接转到“自定义用户控件”中的“ QuantityBox_TextChanged”。

How can i send the SelectedIndex of the item of which i am trying to change the quantity? 如何发送我要更改数量的商品的SelectedIndex?

EDIT-----------------------------CODE of Custom Control// 编辑---------------------------------自定义控件的代码//

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();
        }
    }
}

Dont worry, We will get you the solution. 不用担心,我们将为您提供解决方案。 If Quantity_TextChanged method is raised, just add these lines in your Quantity TextBox. 如果引发了Quantity_TextChanged方法,只需将这些行添加到“数量”文本框中。

<TextBox IsHitTestVisible="False" />

Now you will be redirected to listbox_SelectionChanged method. 现在,您将被重定向到listbox_SelectionChanged方法。 There, you will get the selected index of the listbox. 在那里,您将获得列表框的选定索引。 Now raise the keyboard using, 现在抬起键盘,

QuantityTextBox.Focus();

Now again get the new value from the user, replace the old value with the new value using the selected index of the listbox and display the value again. 现在再次从用户那里获得新值,使用列表框的选定索引将旧值替换为新值,然后再次显示该值。 Its simple right :) 它简单的权利:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM