Skip to main content

Microsoft Silverlight

Answered Question Binding of ListBox ?RSS Feed

(0)

timor.super2
timor.su...

Member

Member

118 points

75 Posts

Binding of ListBox ?

Hi all,

I have a listBox :

 

<ListBox x:Name="myList" Grid.Column="2" Height="400" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid  Width="400">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <Rectangle Stroke="Black"/>
                        <TextBlock Text="{Binding Item1}" Margin="5" Grid.Column="0"/>
                        <Rectangle Stroke="Black" Grid.Column="1"/>
                        <TextBlock x:name="theItem2" Text="{Binding Item2}" Margin="15" FontSize="10" Grid.Column="1"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
</ListBox>

 binded with myList.ItemsSource = query with linq

Some times, depending on the item binded, i would like to hide theItem2.

But I can't find ItemCreated event or something else

 

How can I do ?

Thanks for your help 

brianleahy
brianleahy

Member

Member

28 points

5 Posts

Answered Question

Re: Binding of ListBox ?

You may want to bind the visibility of the item2 texblock to a bool of somesort indicating if it is to be shown.  You will need a bool to visibility converter though.

sladapter
sladapter

All-Star

All-Star

17439 points

3,172 Posts

Answered Question

Re: Binding of ListBox ?

 You can also do the check in TextBlock.Loaded event.

 

sladapter
Software Engineer
Aprimo, Inc

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

timor.super2
timor.su...

Member

Member

118 points

75 Posts

Re: Binding of ListBox ?

Thanks for your answer, this is working well

 

I've another question :

with the same listbox : 

<ListBox x:Name="myList" Grid.Column="2" Height="400" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid x:Name="theGrid" Width="400">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <Rectangle Stroke="Black"/>
                        <TextBlock Text="{Binding Item1}" Margin="5" Grid.Column="0"/>
                        <Rectangle Stroke="Black" Grid.Column="1"/>
                        <TextBlock x:name="theItem2" Text="{Binding Item2}" Margin="15" FontSize="10" Grid.Column="1"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
</ListBox> 

I'm adding a name for the grid : theGrid

How can I access each dataTemplate in codebehing to manipulate the grid ?

(for example, after the grid has been constructed ...)

 

Thanks for your answer 

timor.super2
timor.su...

Member

Member

118 points

75 Posts

Answered Question

Re: Binding of ListBox ?

 As you teached me, I've done it with the Loaded event of the grid, but is there another way ?

sladapter
sladapter

All-Star

All-Star

17439 points

3,172 Posts

Re: Binding of ListBox ?

I don't think you can access the controls in the ListItem by referring name because the item in list is repeated item.  So giving them a name does not do anything. The only way to access the item I found so far (in this beta1 release) is in the event handler's sender. I don't know what you want to achieve. There might be easy workaround for what you want to do.

 

 

sladapter
Software Engineer
Aprimo, Inc

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

timor.super2
timor.su...

Member

Member

118 points

75 Posts

Re: Binding of ListBox ?

 This is enough for what I want to do.

Thanks for you help Wink 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities