【问题标题】:No table in windows phone 8windows phone 8中没有表格
【发布时间】:2014-04-26 03:17:32
【问题描述】:

我想在 wp8 中将 3 个字符串数据绑定到表中,但不知道该怎么做。

问题是:- 我从服务器得到响应然后我将此响应解绑到字符串的例如字符串 a、字符串 b、字符串 c 我想将它们作为行显示在手机屏幕上,然后当我得到第二个响应时,我想像旧字符串一样将它们绑定在新行中(保留旧行)......以获得完整的表格

请帮助我,我尝试过这样的示例,但手机屏幕上没有出现任何数据:-

Grid Layout = new Grid();
Layout.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
Layout.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(50) });
Layout.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50) });
Layout.Children.Add(new TextBlock() { });
//Read the children
foreach (UIElement element in Layout.Children)
{
    //Read it's row and column property
    int row = (int)element.GetValue(Grid.RowProperty);
}
//Some logic to store the row-columns of the UIElement, if required

//Then bind some data by retrieving the TextBlock
TextBlock lbl = Layout.Children[0] as TextBlock;
Binding bind = new Binding();
//logic for binding
lbl.SetBinding(TextBlock.TextProperty, bind);

【问题讨论】:

  • 我已经回到原来的问题,因为它比进一步编辑更好地描述了问题。

标签: c# wpf silverlight windows-phone-8 user-controls


【解决方案1】:

您的代码无法正常工作的原因有以下几点:

  • 您没有为 TextBlocks 设置 Column - Grid.SetColumn()
  • 您正在创建 Grid,但没有将其添加到屏幕上可见的任何 StackPanel 或 ListBox 中
  • 我不确定您尝试在 foreach 循环中实现什么
  • 很难说出您的“绑定逻辑”是做什么的

在这种情况下,使用 ListBox 会更简单:

在 XAML 中,在您的页面中:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
   <ListBox Name="myList">
       <ListBox.ItemContainerStyle>
           <Style TargetType="ListBoxItem">
               <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
           </Style>
       </ListBox.ItemContainerStyle>
       <ListBox.ItemTemplate>
           <DataTemplate>
               <Grid>
                   <Grid.ColumnDefinitions>
                       <ColumnDefinition Width="1*"/>
                       <ColumnDefinition Width="1*"/>
                       <ColumnDefinition Width="1*"/>
                   </Grid.ColumnDefinitions>
                   <TextBlock Text="{Binding textFirst}" Grid.Column="0" HorizontalAlignment="Left"/>
                   <TextBlock Text="{Binding textSecond}" Grid.Column="1" HorizontalAlignment="Center"/>
                   <TextBlock Text="{Binding textThird}" Grid.Column="2" HorizontalAlignment="Right"/>
               </Grid>
           </DataTemplate>
       </ListBox.ItemTemplate>
   </ListBox>
</Grid>

在后面的代码中 - 您只需为项目声明 ObservableCollection 并将其设置为 ListBox 的 ItemsSource。您还需要使用您的数据创建一个类:

public class Data
{
    public string textFirst { get; set; }
    public string textSecond { get; set; }
    public string textThird { get; set; }
}

public partial class MainPage : PhoneApplicationPage
{
    ObservableCollection<Data> dataReceived = new ObservableCollection<Data>();

    public MainPage()
    {
        InitializeComponent();

        myList.ItemsSource = dataReceived;
        // and to add data you do it like this:
       dataReceived.Add(new Data() { textFirst = "First text", textSecond = "Second Text", textThird = "Third one" });
    }
 }

【讨论】:

  • 对不起朋友它不起作用, 上的错误,我已经用我需要绑定的代码更新了我的问题,请告知...
  • @user3451041 什么样的错误? (可能您没有为 ObservaleCollection 添加命名空间 - 然后添加 using System.Collections.ObjectModel; 以及其他用途)。如果你得到这个工作,那么你可以轻松地将你的数据放入 3 列表中。
  • @user3451041 那么错误说明了什么?您可以尝试仅使用我发布的 ListBox 构建一个非常简单的应用程序 - 看看它是如何工作的?
  • 我试过它给我的错误,请你有其他场景
  • @user3451041 仍然 - 你能说出你得到什么样的错误吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-22
  • 1970-01-01
相关资源
最近更新 更多