2016-09-22 80 views
0

我使用數據綁定。數據綁定有點麻煩UWP(C#)

我有這些類:

public class Billing 
    { 
     public string first_name { get; set; } 
     public string last_name { get; set; } 
     public string company { get; set; } 
     public string address_1 { get; set; } 
     public string address_2 { get; set; } 
     public string city { get; set; } 
     public string state { get; set; } 
     public string postcode { get; set; } 
     public string country { get; set; } 
     public string email { get; set; } 
     public string phone { get; set; }   
    } 

    public class Shipping 
    { 
     public string first_name { get; set; } 
     public string last_name { get; set; } 
     public string company { get; set; } 
     public string address_1 { get; set; } 
     public string address_2 { get; set; } 
     public string city { get; set; } 
     public string state { get; set; } 
     public string postcode { get; set; } 
     public string country { get; set; } 
    } 

    public class RootObject 
    { 
     public int id { get; set; } 
     public int parent_id { get; set; } 
     public string status { get; set; } 
     public string order_key { get; set; } 
     public string currency { get; set; } 
     public string version { get; set; } 
     public bool prices_include_tax { get; set; } 
     public string date_created { get; set; } 
     public string date_modified { get; set; } 
     public int customer_id { get; set; } 
     public double discount_total { get; set; } 
     public double discount_tax { get; set; } 
     public double shipping_total { get; set; } 
     public double shipping_tax { get; set; } 
     public double cart_tax { get; set; } 
     public double total { get; set; } 
     public double total_tax { get; set; } 
     public Billing billing { get; set; } 
     public Shipping shipping { get; set; } 
     public string payment_method { get; set; } 
     public string payment_method_title { get; set; } 
     public string transaction_id { get; set; } 
     public string customer_ip_address { get; set; } 
     public string customer_user_agent { get; set; } 
     public string created_via { get; set; } 
     public string customer_note { get; set; } 
     public string date_completed { get; set; } 
     public string date_paid { get; set; } 
     public string cart_hash { get; set; } 
     public List<object> line_items { get; set; } 
     public List<object> tax_lines { get; set; } 
     public List<object> shipping_lines { get; set; } 
     public List<object> fee_lines { get; set; } 
     public List<object> coupon_lines { get; set; } 
    } 

我嘗試使用數據綁定這樣的:

RestAPI rest = new RestAPI("http://simplegames.com.ua/wp-json/wc/v1/", "ck_9d64c027d2c5f81b8bed3342eeccc6d337be813d", "cs_60697b1e6cbdeb8d62d19e0765e339f8e3334754"); 
     WCObject wc = new WCObject(rest); 
     //Get all products 
     var orders = await wc.GetOrders(new Dictionary<string, string>() { 
      { "per_page", "100" }}); 
     string products = orders.ToFormattedJsonString(); 
     List<RootObject> rootObjectData = JsonConvert.DeserializeObject<List<RootObject>>(products); 
     foreach (RootObject root in rootObjectData) 
     { 
      string date = root.date_created; 
      string name = root.billing.first_name + root.billing.last_name; 
      Orders = new ObservableCollection<RootObject> { new RootObject { date_created = date, billing = name },     
      }; 
      OrdersGridView.ItemsSource = rootObjectData;     
     } 
    } 

我需要綁定的名字,但它在結算類。我如何做到這一點?

當我明白我需要從計費

我嘗試使用billing = name接收數據,但我有這個錯誤

錯誤CS0029無法隱式轉換類型「字符串」到「Milano.InWork.Billing 「

這是我的XAML:

<GridView x:Name="OrdersGridView" > 
      <GridView.ItemTemplate> 
       <DataTemplate> 
        <StackPanel> 
         <TextBlock Text="{Binding date_created}" Foreground="#FFFFFDFD" /> 
         <TextBlock Text="{Binding billing}"/> 

        </StackPanel> 
       </DataTemplate> 
      </GridView.ItemTemplate> 

非常感謝您的幫助!

+0

我想你需要看看數據綁定如何在xaml中工作。由於這不是你怎麼做的https://msdn.microsoft.com/en-us/library/ms752347%28v=vs.110%29.aspx –

+0

hm。我不能使用Observable集合?或者是什麼。我看UWP中的數據綁定教程@FilipCordas – Eugene

+0

對於'date_created'代碼工作@FilipCordas – Eugene

回答

1

我沒有在Bill類中找到name屬性,我認爲你真正想要綁定的是賬單的first_namelast_name屬性。

對於綁定這些,只需使用<TextBlock Text="{Binding billing.first_name}"/>而不是billing=name,它將工作。

bill是綁定到GridViewrootObjectData的子集,我們不能直接收集綁定到TextBlock,我們需要.符號獲得集合中的屬性之一。

關於uwp中數據綁定的更多細節請參考this documents。順便說一下,我已經看到了這個線程中的註釋,WPF中的數據綁定與uwp中的數據綁定並不完全相同,請參考上述文檔以進一步學習,您也可以下載official sample進行進一步測試。

+0

謝謝你的傢伙。你幫我! – Eugene