前端代码:
也页面的xaml中引入ObjectDataProvider:
<Window.Resources>
<ResourceDictionary>
<ObjectDataProvider x:Key="GoodsDetail"/>
</ResourceDictionary>
</Window.Resources>
然后在文本框里面绑定数据:
<TextBox Margin="10" Text="{Binding Source={StaticResource GoodsDetail},Path=temp}"/>
<TextBox Margin="10" Text="{Binding Source={StaticResource GoodsDetail},Path=temp1}"/>
这里添加一个button,用于检测提交后的ObjectDataProvider
<Button Content="提交信息" Margin="10" Click="Button_Click"/>
后端代码:
1.在xaml.cs的后台代码中,设置一个myObject的属性
public ObjectDataProvider myObject
{
get
{
return TryFindResource("GoodsDetail") as ObjectDataProvider;//找到前端设置的资源文件
}
}
2.创建一个OrderInfo类,用于接受myObject.ObjectInstance对象
public class OrderInfo
{
public string temp { get; set; }
public string temp1 { get; set; }
}
3.这一步也是最重要的部分,在页面初始化的时候,初始化myObject.ObjectInstance。
public MainWindow()
{
InitializeComponent();
OrderInfo obj = new OrderInfo()
{
temp = "11da",
temp1="admin"
};
setData(obj);
}
public void setData(OrderInfo obj)
{
myObject.ObjectInstance = obj;
}
最后是button事件的debug检验,这个需要自己打断点查看
private void Button_Click(object sender, RoutedEventArgs e)
{
OrderInfo obj= myObject.ObjectInstance as OrderInfo;
}