将代码数据绑定到 XAML 透视列表框

Posted

技术标签:

【中文标题】将代码数据绑定到 XAML 透视列表框【英文标题】:Binding code data to XAML pivot listbox 【发布时间】:2015-12-14 11:02:13 【问题描述】:

我正在为 Windows Phone 7.1 处理数据绑定问题。我有一个DataLoader 类,其中OnservableCollectionItemList(自定义类)作为属性。因此,每个数据透视项目都有自己的项目列表。在这个DataLoader 中,我从一个 JSON 加载数据到一个 JSON。到目前为止,一切顺利。

public ObservableCollection<ItemList> PivotItem  get; set; 

在这里,我存储了五个ItemList,一个用于每个数据透视表头及其对应的项目列表。

但我的问题是我想在每个 PivotItem 中使用 ListBox 将此数据绑定到 XAML。

<phone:Pivot Title="iMetrópolis" Loaded="Pivot_Loaded">
        <!--Elemento Pivot 1-->
        <phone:PivotItem 
            x:Uid="PivotItem1"
            Header="Todo" Margin="14,10,10,18">

            <ListBox x:Name="FirstListBox" ItemsSource="Binding Items">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,0,17" Width="432" Height="78">
                        // I want to add textboxes binding my data
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </phone:PivotItem>
        .
        .
        .

感谢回复!!

【问题讨论】:

【参考方案1】:

这是我认为你需要做的,一个数据绑定的例子

        <ListBox x:Name="listBox1">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Height="auto" >
                        <TextBlock Text="Binding PON"  />
                        <TextBlock Text="Binding PIN" />

                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

还有一个类

   public class ListObject
    
        public string PON  get; set; 
        public string PIN  get; set; 
    

并从 json 中绑定真实数据

     dynamic json = JsonConvert.DeserializeObject(jsondata);

     var questions = new List<ListObject>();
      foreach (var abc in json["jsonarray"])
                
     var listOfItems = new ListObject();
listOfItems.PON= abc.object1;
listOfItems.PIN= abc.object2;
 questions.Add(listOfQuestions);



  listBox1.ItemsSource = questions;

我希望它有助于回复任何 cmets

【讨论】:

感谢您的回复!但是您将questions 用作ItemsSource 用于ListBox,但是PON 和PIN 从另一个来源获取它们的值,而不是questions 对象(?)中的内容。好吧,我有(例如标题(字符串)列表,我想将其用作我的ItemsSource,但我想在 PON TextBox 中显示每个字符串。 嗨,伙计!我已经了解了整个绑定问题。您的回复确实很有帮助!

以上是关于将代码数据绑定到 XAML 透视列表框的主要内容,如果未能解决你的问题,请参考以下文章