Skip to main content

Microsoft Silverlight

Answered Question Data binding - List collection item definition errorRSS Feed

(0)

silverlightdesigner
silverli...

Member

Member

4 points

19 Posts

Data binding - List collection item definition error

Hi all,

With this simple List collection I can data bind it to TextBlocks with no problem:

stories = new List<StoryInfo>();

stories.Add(new StoryInfo() { Headline = "US Gold Haul", Body = "Speed skating becomes a sensation at the winter olympics as US pick up seven gold medals in four days."Image = new BitmapImage(new Uri("Images/1lg.png", UriKind.Relative)) });

However, when I add another item 'TypeBody' I get an error message on application build telling me that my class ComponentOne.StoryInfo does not contain a definition for 'TypeBody' (shown below):

stories = new List<StoryInfo>();

stories.Add(new StoryInfo() { Headline = "US Gold Haul", Body = "Speed skating becomes a sensation at the winter olympics as US pick up seven gold medals in four days.", TypeBody = "Play video", Image = new BitmapImage(new Uri("Images/1lg.png", UriKind.Relative)) });

Any ideas why this item is generating this error, I added it to my List collection the same as the other three items, Heading, Body and Image, and they all bind correctly?

Thanks for any help. 

 

Nick Barling

Pravinkumar R. D.
Pravinku...

Contributor

Contributor

4300 points

708 Posts

Re: Data binding - List collection item definition error

Hi,

Can you share the code for StoryInfo class?

Thanks,

Pravin

 

silverlightdesigner
silverli...

Member

Member

4 points

19 Posts

Re: Data binding - List collection item definition error

Here is the full code:

In MainPage.xaml.cs:

 

namespace OlympicMediaComponentOne

{

public partial class MainPage : UserControl

{

private List<StoryInfo> stories;private StoryInfo selectedStory;

 

public MainPage()

{

// Required to initialize variables

InitializeComponent();

stories =
new List<StoryInfo>();

stories.Add(new StoryInfo() { Headline = "US Gold Haul", Body = "Speed skating becomes a sensation at the winter olympics as US pick up seven gold medals in four days.", TypeBody = "Play video", Image = new BitmapImage(new Uri("Images/1lg.png", UriKind.Relative)) });

stories.Add(new StoryInfo() { Headline = "Big Three Qualify", Body = "High drama as US, Russia and Canada finish top of their respective qualifying groups.", Image = new BitmapImage(new Uri("Images/2lg.png", UriKind.Relative)) });

stories.Add(new StoryInfo() { Headline = "Whistler Landslide", Body = "Downhill skiers face huge delays as main run is blocked by landslide on day four of competition.", Image = new BitmapImage(new Uri("Images/3lg.png", UriKind.Relative)) });

stories.Add(new StoryInfo() { Headline = "Alonso Record Gold", Body = "German pair battle for silver as Spanard Alonso skies into record books with first sub 30 minute finish in 35k final.", Image = new BitmapImage(new Uri("Images/4lg.png", UriKind.Relative)) });

stories.Add(new StoryInfo() { Headline = "Duel for Gold", Body = "Cross country sprint finish is the first to result in a dead heat for 16 years. Swede and Austrian cross the line in record...", Image = new BitmapImage(new Uri("Images/5lg.png", UriKind.Relative)) });

stories.Add(new StoryInfo() { Headline = "Greek Qualifies", Body = "As the first female alpine skier to make Greece's national youth team, Marilena Michou has been carrying a torch for...", Image = new BitmapImage(new Uri("Images/6lg.png", UriKind.Relative)) });

stories.Add(new StoryInfo() { Headline = "Cologna Resumes Training", Body = "World Cup cross country skiing champion Dario Cologna of Switzerland has resumed training on snow following a...", Image = new BitmapImage(new Uri("Images/7lg.png", UriKind.Relative)) });

stories.Add(new StoryInfo() { Headline = "Davis and White Lead", Body = "Meryl Davis and Charlie White hold an almost 15-point lead after the original dance at the Olympic center rink.", Image = new BitmapImage(new Uri("Images/8lg.png", UriKind.Relative)) });

ImageListBox.ItemsSource = stories;

DetailsPanel.ItemsSource = stories;

TransitionDetails.ItemsSource = stories;

 

 

}

private void OnThumbSelectionChanged(object sender, SelectionChangedEventArgs e)

{

selectedStory = (StoryInfo)ImageListBox.SelectedItem;

if (selectedStory != null)

{

ImageListBoxCover.Visibility = Visibility.Visible;

TransitionPanel.DataContext = selectedStory;

TransitionDetails.SelectedItem = selectedStory;

IEnumerable<DependencyObject> containers = ImageListBox.GetContainers();

FrameworkElement container = GetContainerForItem(ImageListBox, selectedStory);

GeneralTransform transform = container.TransformToVisual(MainPanel);

Point location = transform.Transform(
new Point(0, 0));

TransitionLeftAnimation.From = location.X;

TransitionTopAnimation.From = location.Y;

TransitionStoryboard.Begin();

}

}

In detailsPanel.xaml (where the xaml control are bound):

 

<StackPanel Orientation="Vertical" UseLayoutRounding="True" Margin="0" d:LayoutOverrides="HorizontalMargin" HorizontalAlignment="Left" VerticalAlignment="Top" Width="216" MinHeight="75">
   <Grid>
    <TextBlock x:Name="DetailsHeadlineCrossFade" Foreground="White" FontFamily="Arial" FontSize="12" Margin="8,6,4,0" TextWrapping="Wrap" FontWeight="Bold" />
    <TextBlock x:Name="DetailsHeadline" Text="{Binding Headline}" Foreground="White" FontFamily="Arial" FontSize="12" Margin="8,6,4,0" TextWrapping="Wrap" FontWeight="Bold" />
   </Grid>
   <Grid Margin="0,0,0,11">
    <TextBlock x:Name="DetailsBodyCrossFade" Foreground="White" FontFamily="Arial" FontSize="10.667" Margin="8,4,4,0" TextWrapping="Wrap" />
    <TextBlock x:Name="DetailsBody" Text="{Binding Body}" Foreground="White" FontFamily="Arial" FontSize="10.667" LineHeight="13" Margin="8,4,8,0" TextWrapping="Wrap" />
   </Grid>
   <Grid>
    <TextBlock x:Name="DetailsMedia" Text="{Binding TypeBody}" Margin="8,0,0,0" FontFamily="Arial" FontSize="10.667" Foreground="White" TextWrapping="Wrap"/>
   </Grid>
   
  </StackPanel> 

I hope that is enough code?

 

 

Nick Barling

Mog Liang - MSFT
Mog Lian...

All-Star

All-Star

16023 points

1,553 Posts

Re: Data binding - List collection item definition error

Hi,

The compile error means your class "StoryInfo" doesn't have property or field called "TypeBody". You need check you StoryInfo type definiton, if "TypeBody" is needed, add the field to your class definition.

Thanks,

Mog Liang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

silverlightdesigner
silverli...

Member

Member

4 points

19 Posts

Answered Question

Re: Data binding - List collection item definition error

 Mog,

 

Thank you, that is exactly what the problem was, the 'TypeBody' was not defined in the StoryInfo class .cs file, which was my mistake.

 

Thanks for pointing that out!

Nick Barling
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities