Question

i want to bind 3 string data to table in wp8 but idont know how to do that.

the problem is :- i get response from the server then i desrlaized this response to string's for example string a, string b , string c i want to display them on the screen of the phone as row then when i got the second response i want to bind them in new line like the old string (Keep the old row)...... to get full table

please help me i have tried sample like this but there in no data appeared on the phone screen :-

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);
Was it helpful?

Solution

The are couple of things why your code doesn't work:

  • you are not setting Column for your TextBlocks - Grid.SetColumn()
  • you are creating Grid, but you aren't adding it to to any StackPanel or ListBox which are visible on the screen
  • I'm not sure what you have tried to achieve within foreach loop
  • it is hard to tell what you 'logic for binding' does

In this case it is simpler to use a ListBox:

In XAML, in your Page:

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

In code behind - you just then declare ObservableCollection for items and set it as ItemsSource of ListBox. You need also to create a class with your data:

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" });
    }
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top