从另一个窗口访问类数据

时间:2015-08-12 11:47:38

标签: c# wpf linq wcf class

我有两个窗户。在第一个窗口中,我将数据插入到类QuoteData中,编码如下

using (TruckServiceClient client = new TruckServiceClient())
{
     quoteFinalize = await client.GetQuoteAsync(new QuoteData
     {
         BodyTypeId = cmbBodyType.GetDisplayItemId(),
         ChassisId = cmbChassisCab.GetDisplayItemId(),
         FloorId = cmbFloorSpecification.GetDisplayItemId(),
         ExternalLength = externalLength,
         ExternalWidth = externalWidth,
         ExternalHeight = externalHeight
});

然后在第二个窗口中,我想访问我插入数据的同一个类(QuoteData),然后将ExternalLength QuoteData设置为我的标签名为lblExternalLengthAmount

示例:lblExternalLengthAmount.Content = ExternalLength;

我已尝试在第二个窗口中创建QuoteData的新实例,但所有值都重新显示为 null

有没有办法访问这些值?任何建议将不胜感激:)

1 个答案:

答案 0 :(得分:1)

您可以将构造函数中的Window1对象传递给Window2

Window1.xaml.cs

 Window2 dialog = new Window2(this);//this is current window(Window1) object

并在Window2.xaml.cs

public Window2(Window1 obj)
        {

            InitializeComponent();
            //obj is your Window1 object
        }

对obj所做的任何更改都将反映到Window1