locked
WPF data binding RRS feed

  • Question

  • I would like to know how to have a text box on the form and binding to my data?

    Your help and information is great appreciated,

    Sourises,

    • Changed type sourises Saturday, September 22, 2012 10:57 PM
    • Moved by Youen Zen Monday, September 24, 2012 5:35 AM To provide better support (From:Visual Basic Express Edition)
    Saturday, September 22, 2012 10:22 PM

Answers

  • Hello,

    To do that you need to bind ItemSource of your Listbox, Datagrid... to an ObservableCollection<Data> in the backend.

    When the binding is done, you need to update your collection by retrieving the data from your database and push it to your ObservableCollection and call OnPropertyChanged of your element.

    This should automatically update your elements in the XAML element.

    Regards,

    Linvi

    • Marked as answer by sourises Thursday, October 4, 2012 1:42 AM
    Monday, September 24, 2012 7:15 PM

All replies

  • I have to use XAML to set data binding for WPF applications.

    Am I right?

    Thanks again for your informationb and help,

    Sourises,

    Sunday, September 23, 2012 2:12 AM
  • Hi Sourises,

    This topic is related to WPF. We have a WPF forum for this, in order to provide better support I will move this thread.

    Thanks for understanding!


    Shanks Zen
    MSDN Community Support | Feedback to us

    Monday, September 24, 2012 5:35 AM
  • Hi sourises,

    To bind with textbox, we need a bindable dataSource first, such as:

    private string mytext = "Hello";
            public dostring text
            {
                get { return this.mytext; }
                set
                {
                    if (value != this.mytext)
                    {
                        this.mytext = value;
                        NotifyPropertyChanged("Mytext");
                    }
                }
            }
     public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }

    Then to set the textbox's datacontext as the window's context as myTextbox.DataContext = this;

    Last to bind textbox in xaml as:

     <TextBox Text="{Binding Path=Mytext}" Name="myTB" Width="100" Height="30" Background="AliceBlue"/>

    For more detail information about binding in WPF, you can check out:http://msdn.microsoft.com/en-us/library/ms752347.aspx

    Hope it helps.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

    Monday, September 24, 2012 7:08 AM
  • Thanks for the message,

    It binds text from code. How can I bind from a database table to read from and update?

    Thanks again for helping,

    Sourises,

    Monday, September 24, 2012 4:08 PM
  • Hello,

    To do that you need to bind ItemSource of your Listbox, Datagrid... to an ObservableCollection<Data> in the backend.

    When the binding is done, you need to update your collection by retrieving the data from your database and push it to your ObservableCollection and call OnPropertyChanged of your element.

    This should automatically update your elements in the XAML element.

    Regards,

    Linvi

    • Marked as answer by sourises Thursday, October 4, 2012 1:42 AM
    Monday, September 24, 2012 7:15 PM
  • Thanks for the information,

    Can you please give me one exmaple in coding?

    Thanks again,

    Regards,

    Sourises,

    Monday, September 24, 2012 7:42 PM