0

txtidtxtName、、、..OnSelectionChange を更新する必要がありますがtxtAge、項目を選択した後でも更新されません。どうすればステップアップできますか?txtContactListPickertxtfields

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 要素を selectedItem にバインドしました。selectedItem の値が変更されると、Notify イベントがトリガーされ、UI に通知され、値が自動的に更新されます。

ビューモデルを勉強して実装することをお勧めします。それは本当に簡単で、多くの手間を省くことができます。

サンプルをいくつか実装してみる

それが役に立てば幸い :)

于 2012-10-01T05:03:20.880 に答える
0

私はこれを手に入れました...実際に更新したい場合は、各テキストボックスのテキストを変更する必要があります。選択変更後、このようにしてみてください。

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);
            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 に答える