2010-11-17 3 views
-1

Mise à jour pour Matt Lacey :: C# CodeDatabinding aucune erreur, mais la liaison ne sort

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using Microsoft.Phone.Controls; 
using System.Collections.ObjectModel; 

namespace WindowsPhoneApplication1 
{ 
    public partial class MainPage : PhoneApplicationPage 
    { 
     public MainPage() 
     { 
      InitializeComponent(); 
     } 

     private void PhoneApplicationPage_Loaded(object sender, System.Windows.RoutedEventArgs e) 
     { 
      var comments = new Comments(); 

      var threeComments = new List<Comment> 
           { 
            new Comment("t1", "d1", "n1", "i1"), 
            new Comment("t2", "d2", "n2", "i3"), 
            new Comment("t3", "d3", "n3", "i3") 
           }; 

      comments.comments = threeComments.ToArray(); 

      commentsLooper.ItemsSource = new CommentsDataItems(comments);   
     } 
    } 

    public class Comments 
    { 
     public Comment[] comments { get; set; } 
    } 

    public class CommentsDataItems : ObservableCollection<CommentDataItem> 
    { 
     public CommentsDataItems(Comments comments) 
     { 
      foreach (var com in comments.comments) 
      { 
       Add(new CommentDataItem(com.text, com.device, com.name, com.id)); 
      } 
     } 
    } 

    public class CommentDataItem 
    { 
     public String text { get; set; } 
     public String device { get; set; } 
     public String name { get; set; } 
     public String id { get; set; } 

     public CommentDataItem(String text, String device, String name, String id) 
     { 
      this.text = text; 
      this.device = device; 
      this.name = name; 
      this.id = id; 
     } 
    } 

    public class Comment 
    { 
     public String text { get; set; } 
     public String device { get; set; } 
     public String name { get; set; } 
     public String id { get; set; } 

     public Comment(String text, String device, String name, String id) 
     { 
      this.text = text; 
      this.device = device; 
      this.name = name; 
      this.id = id; 
     } 
    } 
} 

Code XAML:

<phone:PhoneApplicationPage 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="800" 
    x:Class="WindowsPhoneApplication1.MainPage" 
    FontFamily="{StaticResource PhoneFontFamilyNormal}" 
    FontSize="{StaticResource PhoneFontSizeNormal}" 
    Foreground="{StaticResource PhoneForegroundBrush}" 
    SupportedOrientations="Portrait" Orientation="Portrait" 
    shell:SystemTray.IsVisible="True" Loaded="PhoneApplicationPage_Loaded"> 

    <!--LayoutRoot is the root grid where all page content is placed--> 
    <Grid x:Name="LayoutRoot" Background="Transparent"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 

     <!--TitlePanel contains the name of the application and page title--> 
     <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
      <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Foreground="White"/> 
      <TextBlock x:Name="PageTitle" Text="Test Page" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" Foreground="White"/> 
     </StackPanel> 

     <!--ContentPanel - place additional content here--> 
     <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
      <ItemsControl x:Name="commentsLooper" Grid.Row="1"> 
       <ItemsControl.ItemTemplate> 
        <DataTemplate> 
         <TextBlock Height="100" Margin="0" TextWrapping="Wrap" Text="{Binding Text}"/> 
        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 
     </Grid> 
    </Grid> 
</phone:PhoneApplicationPage> 
+0

À quoi 'commentsLooper' est-il lié? – slugster

+0

Jetez un coup d'œil à la fenêtre de sortie VS2010. Normalement, vous recevrez un avertissement de spam si une liaison échoue. –

+0

non désolé pas de sortie, et il est lié à rien – Barkermn01

Répondre

0

J'ai dû deviner ce que certains de vos les types sont mais les travaux suivants pour moi:
Notez que j'ai légèrement renommé les choses et tout mettre dans le même espace de noms

 var comments = new Comments(); 

     var threeComments = new List<Comment> 
           { 
            new Comment("t1", "d1", "n1", "i1"), 
            new Comment("t2", "d2", "n2", "i3"), 
            new Comment("t3", "d3", "n3", "i3") 
           }; 

     comments.comments = threeComments.ToArray(); 

     commentsLooper.ItemsSource = new CommentsDataItems(comments); 


public class Comments 
{ 
    public Comment[] comments { get; set; } 
} 

public class CommentsDataItems : ObservableCollection<CommentDataItem> 
{ 
    public CommentsDataItems(Comments comments) 
    { 
     foreach (var com in comments.comments) 
     { 
      Add(new CommentDataItem(com.text, com.device, com.name, com.id)); 
     } 
    } 
} 

public class CommentDataItem 
{ 
    public String text { get; set; } 
    public String device { get; set; } 
    public String name { get; set; } 
    public String id { get; set; } 

    public CommentDataItem(String text, String device, String name, String id) 
    { 
     this.text = text; 
     this.device = device; 
     this.name = name; 
     this.id = id; 
    } 
} 

public class Comment 
{ 
    public String text { get; set; } 
    public String device { get; set; } 
    public String name { get; set; } 
    public String id { get; set; } 

    public Comment(String text, String device, String name, String id) 
    { 
     this.text = text; 
     this.device = device; 
     this.name = name; 
     this.id = id; 
    } 
} 

Dans le XAML j'ai apporté des modifications mineures. Le plus important était de supprimer les hauteurs et les largeurs explicites. Notamment parce que vous définissez une petite hauteur relationnelle sur la grille, il n'y avait que la place pour voir un élément.

<Grid x:Name="commentsCotainer" VerticalAlignment="Top"> 
    <ItemsControl x:Name="commentsLooper" Grid.Row="1"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Grid x:Name="comentHolder" HorizontalAlignment="Center" VerticalAlignment="Top"> 
        <TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Top" TextWrapping="Wrap" Text="{Binding text}" Foreground="White" /> 
       </Grid> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
</Grid> 
+0

merci va essayer maintenant. – Barkermn01

+0

Je suis désolé mais j'ai essayé d'ajouter ceci dans mon code mais je ne peux pas trouver tous les changements que vous avez faits? Pourriez-vous simplement les signaler pour moi s'il vous plaît? Comments est un objet qui contient des commentaires [] des commentaires qui sont la même mise en page que le CommentDataItem pourquoi les commentaires sont renvoyés par un lecteur XML qui lit nos serveurs et renvoie l'objet correct pour le code XML approprié – Barkermn01

+0

Sorry i peut voir ceux dans le XAML mais pas le C# – Barkermn01

Questions connexes