Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Unable to Bind a list box with the Results of Linq query..
5 replies. Latest Post by murtaza.dharwala on September 29, 2008.
(0)
murtaza....
Participant
1659 points
378 Posts
09-25-2008 7:50 AM |
Hi I am just about 2 weeks old to the SilverLight World and i have problem with the following task of Binding result to a listbox contorl.
I have employee xml like
<Employees>
<Employee>
<Name></Name>
<EmailId></Name>
<ImageURL></ImageURL>
</Employee>
</Employees>
what i need to do is to show the employees after firing a particular Linq query like
var employees = from employee in element.Elements("Employee") select new CEmployee { Name = employee.Element("FirstName").Value + " " + employee.Element("LastName").Value, EmailId = employee.Element("EmailId").Value, ImageURL = employee.Element("ImageUrl").Value };
here CEmployee is a simple class with 3 string members Name,EmailId,ImageURL
Now the ListBox Item should show the Image (source set by Binding to ImageURl), and Two textblocks containing the Name and the EmailId of the Employee. But i am unable to view any data on the list box item. The Following is the XAML code used by me
<ListBox x:Name="lstEmployee" Grid.Column="0" Grid.Row="0" Margin="0,0,0,0" Width="210" > <ListBox.ItemTemplate> <DataTemplate> <Border> <Border.Background> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FF000000" Offset="1"/> <GradientStop Color="#FF765959" Offset="0"/> <GradientStop Color="#FF564141" Offset="0.451"/> <GradientStop Color="#FF433232" Offset="0.951"/> </LinearGradientBrush> </Border.Background> <Canvas x:Name="ItemCanvas" Height="100" Width="200" > <Image Height="100" Canvas.Top="0" Canvas.Left="0" Width="100" MinWidth="100" MinHeight="100" Source="{Binding ImageUrl}"/> <TextBlock Text="{Binding Name}" x:Name="txbName" Canvas.Top="20" Canvas.Left="100" TextWrapping="Wrap" Foreground="#FFB9ECCD"/> <TextBlock Text="{Binding EmailId}" x:Name="txbEmailId" Canvas.Top="60" Canvas.Left="100" TextWrapping="Wrap" Foreground="#FFB9ECCD"/> </Canvas> </Border> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
Please Suggest some suitable solution to the Problem..
Thanks
HarshBar...
Star
9908 points
1,719 Posts
09-25-2008 8:21 AM |
Hi,
Set Datacontext in your code behind like this.
lstEmployee.DataContext = employees;
make little change in Code behind..
<ListBox x:Name="lstEmployee" Grid.Column="0" Grid.Row="0" Margin="0,0,0,0" Width="210" ItemsSource="{Binding Mode=OneWay}">
sladapter
All-Star
17439 points
3,172 Posts
09-25-2008 10:55 AM |
Did you set the ListBox.ItemsSource? That's how you bind the control to your datalist?
var employees = from employee in element.Elements("Employee")
select new CEmployee
{ Name = employee.Element("FirstName").Value + " " + employee.Element("LastName").Value,
EmailId = employee.Element("EmailId").Value,
ImageURL = employee.Element("ImageUrl").Value
};
lstEmployee.ItemsSource = employees.ToList();
09-26-2008 12:57 AM |
Sorry Harshbardhan , sladapter it is still not working.
what i get is two blank dataitems added to the list box.
so i think the listbox gets the source properly
but there is something missing in the binding field like text block which is being binded like this
<
09-26-2008 8:32 AM |
Check you XML. If this is your XML, then your XML does not contain any data:
Yes, you have two records, but both have empty Name and ImageURL , By the way, <EmailId></Name> is not even valid XML.
Put break point at this line and check the ItemsSource.
09-29-2008 12:57 AM |
Its just an example of structure of the XML not the exact xml.
The problem has been solved Thanks for all your replies