Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Binding of ListBox ?
6 replies. Latest Post by timor.super2 on May 20, 2008.
(0)
timor.su...
Member
118 points
75 Posts
05-19-2008 9:45 AM |
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
28 points
5 Posts
05-19-2008 5:18 PM |
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
All-Star
17439 points
3,172 Posts
05-19-2008 8:11 PM |
You can also do the check in TextBlock.Loaded event.
05-20-2008 8:08 AM |
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
05-20-2008 8:12 AM |
As you teached me, I've done it with the Loaded event of the grid, but is there another way ?
05-20-2008 8:49 AM |
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.
05-20-2008 11:24 AM |
This is enough for what I want to do.
Thanks for you help