Eugene Eugene - 2 months ago 22
C# Question

Data binding little trouble UWP(C#)

I using data binding.

I have those classes:

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; }
}


I try to use data bind like this:

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;
}
}


I need to bind name but it in Billing class. How I can do this?

As I understood I need to receive data from Billing

I try to use
billing = name
but I have this error


Error CS0029 Cannot implicitly convert type 'string' to 'Milano.InWork.Billing'


Here is my xaml:

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

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


Thank's so much for help!

Answer

I didn't find a name property in Bill class, I think what you really want to bind is first_name and last_name properties of the bill.

For bind these, just use <TextBlock Text="{Binding billing.first_name}"/> instead of billing=name, it will work.

The bill is a child collection of the rootObjectData you bind to the GridView, we cannot directly bind a collection to a TextBlock, we need get one of the property in the collection by . symbol.

More details about data binding in uwp please reference this documents. By the way I have seen the comments in this thread, data binding in WPF is not all the same with in uwp, please reference the above documents for further learning and you can also download the official sample for further testing.

Comments