Click here to Skip to main content
15,910,234 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two control main page and child window..there is data grid on main page and on click of edit button of data grid i want to show row item in child window text box..
by using cw.textBox1=mainpage.selectitem its working but i want to used data binding with text box..so how to do it..i try and my code is...
MainPage.xaml.cs
namespace textbind
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            CItem cv = dataGrid1.SelectedItem as CItem;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

        
            Service1Client proxy = new Service1Client();
            proxy.GetItemsCompleted +=new EventHandler<getitemscompletedeventargs>(proxy_GetItemsCompleted);
            proxy.GetItemsAsync();

        }
        void proxy_GetItemsCompleted(object sender, GetItemsCompletedEventArgs e)
        {
            dataGrid1.ItemsSource = e.Result;
        }
        
        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {


            CItem ci = dataGrid1.SelectedItem as CItem;

            ChildWindow1 cw = new ChildWindow1();
            Person p = new Person();


            p.SName = "fdfdF";
         
            cw.Show();
        }
    }
}
</getitemscompletedeventargs>


ChildWindow.xaml.cs
namespace textbind
{
    public partial class ChildWindow1 : ChildWindow
    {
         private Person _model;

         public ChildWindow1()
      {
         InitializeComponent();
         MainPage m = new MainPage();
        CItem c=m.dataGrid1.SelectedItem as CItem;
       // string na = c.SADDRESS.ToString();
        Person person = new Person() { SName = null, SLocation = null };

        this.DataContext = person;
        person.SName = c.SNAME;
      }

      public Person Model
      {
         get { return _model; }
         set
         {
            if(value!=_model)
            {
               _model = value;
               DataContext = _model;
            }
         }
      }

      private void OKButton_Click(object sender, RoutedEventArgs e)
      {

      }

      private void CancelButton_Click(object sender, RoutedEventArgs e)
      {
          this.DialogResult = false;
      }
   }

   public class Person : INotifyPropertyChanged
   {
      private string _name;
      private string _location;
      public event PropertyChangedEventHandler PropertyChanged;

      public string SName
      {
         get { return _name; }
         set
         {
            if(value!=_name)
            {
               _name = value;
               RaisePropertyChanged("SName");
            }
         }
      }

      public string SLocation
      {
         get { return _location; }
         set
         {
            if (value != _location)
            {
               _location = value;
               RaisePropertyChanged("SLocation");
            }
         }
      }

      private void RaisePropertyChanged(string propertyName)
      {
         if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
    }
}
Posted

1 solution

Don't create a new person object but work directly with the selected item. Bind the ChildWindow's DataContext directly to the SelectedItem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900