如何在Silverlight中的两个控件之间访问对象 [英] How to access object between two control in silverlight
本文介绍了如何在Silverlight中的两个控件之间访问对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个控件主页和子窗口..主页上有数据网格,单击数据网格的编辑按钮后,我想在子窗口文本框中显示行项目..
通过使用cw.textBox1 = mainpage.selectitem它的工作,但我想与文本框使用数据绑定..怎么做..我尝试,我的代码是...
MainPage.xaml.cs
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
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));
}
}
}
推荐答案
不要创建新的person对象,但可以直接与所选项目一起使用.将ChildWindow的DataContext直接绑定到SelectedItem.
Don''t create a new person object but work directly with the selected item. Bind the ChildWindow''s DataContext directly to the SelectedItem.
这篇关于如何在Silverlight中的两个控件之间访问对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文