Skip to main content

Microsoft Silverlight

Answered Question Unable to Bind a list box with the Results of Linq query..RSS Feed

(0)

murtaza.dharwala
murtaza....

Participant

Participant

1659 points

378 Posts

Unable to Bind a list box with the Results of Linq query..

 

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>

 

   <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

Murtaza Dharwala
Diaspark Inc.
www.diaspark.com
email:murtaza.dharwala@diaspark.com


Please remember to click “Mark as Answer” on the post that helps you

HarshBardhan
HarshBar...

Star

Star

9908 points

1,719 Posts

Re: Unable to Bind a list box with the Results of Linq query..

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

 

Mark as answer if this post answered your question.

Harsh Bardhan

sladapter
sladapter

All-Star

All-Star

17439 points

3,172 Posts

Re: Unable to Bind a list box with the Results of Linq query..

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();

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

murtaza.dharwala
murtaza....

Participant

Participant

1659 points

378 Posts

Re: Re: Unable to Bind a list box with the Results of Linq query..

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

<TextBlock Text="{Binding Name}" x:Name="txbName" Canvas.Top="20" Canvas.Left="100" TextWrapping="Wrap" Foreground="#FFB9ECCD"/>

Murtaza Dharwala
Diaspark Inc.
www.diaspark.com
email:murtaza.dharwala@diaspark.com


Please remember to click “Mark as Answer” on the post that helps you

sladapter
sladapter

All-Star

All-Star

17439 points

3,172 Posts

Answered Question

Re: Re: Unable to Bind a list box with the Results of Linq query..

Check you XML. If this is your XML, then your XML does not contain any data:

<Employees>

   <Employee>

       <Name></Name>

       <EmailId></Name>

       <ImageURL></ImageURL>

    </Employee>

   <Employee>

       <Name></Name>

       <EmailId></Name>

       <ImageURL></ImageURL>

    </Employee>

</Employees>

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.

 lstEmployee.ItemsSource = employees.ToList();

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

murtaza.dharwala
murtaza....

Participant

Participant

1659 points

378 Posts

Re: Re: Re: Unable to Bind a list box with the Results of Linq query..

Its just an example of structure of the XML not the exact xml.

The problem has been solved Thanks for all your replies

Murtaza Dharwala
Diaspark Inc.
www.diaspark.com
email:murtaza.dharwala@diaspark.com


Please remember to click “Mark as Answer” on the post that helps you
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities