0

我必须更新我的txtid, txtName, txtAge, txtContact..OnSelectionChange ListPicker,但即使在选择项目之后它也不会更新txtfields。我怎样才能加强它?

public partial class Update : PhoneApplicationPage
    {

        Button btnUpdate1 = null;
        TextBox txtid = null;
        TextBox txtName = null;
        TextBox txtAge = null;
        TextBox txtContact = null;
        ListPicker lp = null;
        int selectedItem ;

        public Update()
        {           
            InitializeComponent();
            int x = noofrows();
            createUpdateButton();
            createListPicker();
            selectedItem = Convert.ToInt32(lp.SelectedItem);     
            createTxtId(selectedItem);
            createTxtName(selectedItem);
            createTxtAge(selectedItem);
            createTxtContact(selectedItem);

        }
        public void createListPicker()
        {
            lp = new ListPicker();            
            lp.BorderBrush = new SolidColorBrush(Colors.White);
            lp.BorderThickness = new Thickness(3);
            lp.Margin = new Thickness(12, 5, 0, 0);
            lp.Width = 400;
            int x = noofrows();
            for (int a = 1; a <= x; a++)
            {
                string str1 = returnID(a);
                lp.Items.Add(str1);                
            }
            lp.SelectionChanged += (s, e) =>
            {
                selectedItem = Convert.ToInt32(lp.SelectedItem);

            };
            LayoutRoot.Children.Add(lp);    

        }
        public void createUpdateButton()
        {
            btnUpdate1 = new Button();
            btnUpdate1.Margin = new Thickness(100, 600, 0, 0);
            btnUpdate1.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            btnUpdate1.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
            btnUpdate1.Height = 100;
            btnUpdate1.Width = 150;
            btnUpdate1.Content = "update";
            btnUpdate1.Foreground = new SolidColorBrush(Colors.White);
            btnUpdate1.Background = new SolidColorBrush(Colors.Black);
            btnUpdate1.BorderBrush = new SolidColorBrush(Colors.White);
            btnUpdate1.BorderThickness = new Thickness(3);
            btnUpdate1.FontSize = 28;
            LayoutRoot.Children.Add(btnUpdate1);
            btnUpdate1.Click += (s, e) =>
            {
                UpdateDatabase(selectedItem);
            };

        }
        public void createTxtId(int z)
        {
            int id  = z ;
            txtid = new TextBox();
            txtid.Margin = new Thickness(12, 100, 0, 0);
            txtid.Width = 400;
            txtid.Height = 100;
            txtid.FontSize = 28;
            txtid.Foreground = new SolidColorBrush(Colors.Black);
            txtid.BorderBrush = new SolidColorBrush(Colors.Black);
            txtid.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            txtid.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            txtid.BorderThickness = new Thickness(3);
            txtid.IsReadOnly = true;
            txtid.Text = "  " + id;
            LayoutRoot.Children.Add(txtid);

        }
        public void createTxtName(int z)
        {
            txtName = new TextBox();
            txtName.Margin = new Thickness(12, 200, 0, 0);
            txtName.Width = 400;
            txtName.Height = 100;
            txtName.FontSize = 28;
            txtName.Foreground = new SolidColorBrush(Colors.Black);
            txtName.BorderBrush = new SolidColorBrush(Colors.Black);
            txtName.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            txtName.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            txtName.BorderThickness = new Thickness(3);
            txtName.Text = SelectName(z);
            txtName.Tap += (s, e) =>
            {
                if (txtName.Text == "Name")
                {
                    txtName.Text = "";
                }
            };
            txtName.LostFocus += (s, e) =>
            {
                if (txtName.Text == "")
                {
                    txtName.Text = "Name";
                }
            };
            LayoutRoot.Children.Add(txtName);
        }
        public void createTxtAge(int z)
        {
            txtAge = new TextBox();
            txtAge.Margin = new Thickness(12, 300, 0, 0);
            txtAge.Width = 400;
            txtAge.Height = 100;
            txtAge.FontSize = 28;
            txtAge.Foreground = new SolidColorBrush(Colors.Black);
            txtAge.BorderBrush = new SolidColorBrush(Colors.Black);
            txtAge.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            txtAge.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            txtAge.BorderThickness = new Thickness(3);

            txtAge.MaxLength = 2;

            txtAge.Text = SelectAge(z);
            txtAge.InputScope = new InputScope();
            txtAge.InputScope.Names.Add(new InputScopeName() { NameValue = InputScopeNameValue.Number });
            txtAge.Tap += (s, e) =>
            {
                if (txtAge.Text == "Age")
                {
                    txtAge.Text = "";
                }
            };
            txtAge.LostFocus += (s, e) =>
            {
                if (txtAge.Text == "")
                {
                    txtAge.Text = "Age";
                }
            };
            LayoutRoot.Children.Add(txtAge);
        }
        public void createTxtContact(int z)
        {
            txtContact = new TextBox();
            txtContact.Margin = new Thickness(12, 400, 0, 0);
            txtContact.Width = 400;
            txtContact.Height = 100;
            txtContact.FontSize = 28;
            txtContact.MaxLength = 10;
            txtContact.Foreground = new SolidColorBrush(Colors.Black);
            txtContact.BorderBrush = new SolidColorBrush(Colors.Black);
            txtContact.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            txtContact.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            txtContact.BorderThickness = new Thickness(3);
            txtContact.Text = SelectContact(z);
            txtContact.InputScope = new InputScope();
            txtContact.InputScope.Names.Add(new InputScopeName() { NameValue = InputScopeNameValue.Number });
            txtContact.Tap += (s, e) =>
            {
                if (txtContact.Text == "Contact")
                {
                    txtContact.Text = "";
                }
            };
            txtContact.LostFocus += (s, e) =>
            {
                if (txtContact.Text == "")
                {
                    txtContact.Text = "Contact";

                }
            };
            LayoutRoot.Children.Add(txtContact);

        }
        public Int32 noofrows()
        {
            int b = Convert.ToInt32((Application.Current as App).db.SelectList("select count(*) from details"));
            return b;
        }
        public string returnID(int z)
        {
            return Convert.ToString((Application.Current as App).db.SelectList("select id from details where id =" + z));
        }
        public String SelectName(int x)
        {
            return Convert.ToString((Application.Current as App).db.SelectList("select name from details where id =" + x));
        }
        public String SelectAge(int x)
        {
            return Convert.ToString((Application.Current as App).db.SelectList("select age from details where id =" + x));
        }
        public String SelectContact(int x)
        {
            return Convert.ToString((Application.Current as App).db.SelectList("select contact from details where id =" + x));
        }
        public void  UpdateDatabase(int ID)
        {           
            if (txtName.Text=="Name")
            {
                MessageBox.Show("Enter a valid Name");


            }
            else if (txtAge.Text.Length>=3||txtAge.Text=="Age")
            {
                MessageBox.Show("Enter A valid Age (1-99)");

            }
            else if (txtContact.Text.Length != 10)
            {
                MessageBox.Show("Enter a valid Mobile number Of 10 digit");

            }
            else
            {
                string s1 = "update details set id='" + Convert.ToInt64(txtid.Text) + "', name = '" + txtName.Text + "', age = '" + txtAge.Text + "', contact = '" + txtContact.Text + "'where id = " + ID;
                string s2 = Convert.ToString((Application.Current as App).db.SelectList(s1));

                MessageBox.Show("Updated Successfully");
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            }

        }
    }
4

2 回答 2

1

你正在使用这么多的代码。如果您遵循 MVM 架构并尝试将文本块绑定到您的 viewModel 会更好。让我们考虑以下场景。

您已将 xaml 元素绑定到 viewModel 中的 selectedItem。现在,当 selectedItem 的值发生更改时,会触发 Notify 事件,该事件将通知 UI,并且值将自动更新。

我建议您研究视图模型并尝试实现它。它真的很容易,可以为您省去很多麻烦。

尝试实施一些示例

希望能帮助到你 :)

于 2012-10-01T05:03:20.880 回答
0

我明白了...实际上,如果我们要更新,我们需要更改每个文本框的文本。所以选择改变后,试试这样。

公共无效 createListPicker()

    {
        lp = new ListPicker();            
        lp.BorderBrush = new SolidColorBrush(Colors.White);
        lp.BorderThickness = new Thickness(3);
        lp.Margin = new Thickness(12, 5, 0, 0);
        lp.Width = 400;
        int x = noofrows();
        for (int a = 1; a <= x; a++)
        {
            string str1 = returnID(a);
            lp.Items.Add(str1);                
        }
        lp.SelectionChanged += (s, e) =>
        {
            selectedItem = Convert.ToInt32(lp.SelectedItem);
            txtid.Text = selectedItem.ToString();
            txtName.Text = SelectName(selectedItem);
            txtAge.Text = SelectAge(selectedItem);
            txtContact.Text = SelectContact(selectedItem);
        };
        LayoutRoot.Children.Add(lp);    

    }

这将在每次选择更改事件后立即更新。

于 2012-10-03T03:47:05.967 回答