【问题标题】:Data binding little trouble UWP(C#)数据绑定小麻烦UWP(C#)
【发布时间】:2016-09-22 07:41:17
【问题描述】:

我使用数据绑定。

我有这些课程:

   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 接收数据

我尝试使用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>

非常感谢您的帮助!

【问题讨论】:

  • 我认为您需要研究数据绑定在 xaml 中的工作方式。因为这不是你的做法msdn.microsoft.com/en-us/library/ms752347%28v=vs.110%29.aspx
  • 嗯。我不能使用 Observable 集合?或者是什么。我在 UWP 中观看有关数据绑定的教程 @FilipCordas
  • 对于date_created 代码工作@FilipCordas
  • Windows Presentation Foundation (WPF) data binding provides 这是来自链接。我使用 UWP @FilipCordas
  • 对于所有 xaml 应用程序来说,它都是一样的,WPF 拥有最好的文档。您可以使用 Observable 但它们需要绑定到某些东西 OrdersGridView.ItemsSource = rootObjectData;没有约束力。

标签: c# visual-studio data-binding uwp


【解决方案1】:

我没有在Bill类中找到name属性,我想你真正要绑定的是账单的first_namelast_name属性。

要绑定这些,只需使用&lt;TextBlock Text="{Binding billing.first_name}"/&gt; 而不是billing=name,就可以了。

bill 是您绑定到GridViewrootObjectData 的子集合,我们不能直接将集合绑定到TextBlock,我们需要通过. 获取集合中的一个属性符号。

更多关于uwp中数据绑定的细节请参考this documents。顺便说一下,我在这个线程中看到了cmets,WPF中的数据绑定与uwp中的数据绑定并不完全相同,请参考以上文档进一步学习,也可以下载official sample进行进一步测试。

【讨论】:

  • 谢谢老兄。你帮帮我!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-24
  • 2011-08-19
  • 2015-07-15
  • 2014-01-19
  • 1970-01-01
  • 1970-01-01
  • 2020-07-04
相关资源
最近更新 更多