Skip to main content
Home Forums Silverlight Design Designing with Silverlight [Solved] ListBox Customized Animation Problem
1 replies. Latest Post by Matilas on May 25, 2009.
(0)
Matilas
Member
0 points
2 Posts
05-25-2009 6:50 AM |
Hi everybody,
Firstly, sorry for my english, I'm french and not fluent in english ;)
About my problem... I'm using DataTemplate for my items source, and I'm applying a little "animation" (Visibility Collapsed to Visible) on the event SelectionChanged. When the selection changes, the item removed hides the texblock and the item added shows the texblock, there is no problem.
But, when I select an item, (who was selected before), the texblock doesn't appear... :/
My C# Code
private void list_SelectionChanged(object sender, SelectionChangedEventArgs e) { StackPanel sp = GetChild(newsList) as StackPanel; int index = newsList.Items.ToList().IndexOf(e.AddedItems[0]); if (e.RemovedItems.Count > 0) { int indexToRemove = newsList.Items.ToList().IndexOf(e.RemovedItems[0]); ListBoxItem itemToRemove = sp.Children[indexToRemove] as ListBoxItem; TextBlock textToRemove = GetNewsContent(itemToRemove); textToRemove.Visibility = Visibility.Collapsed; } ListBoxItem item = sp.Children[index] as ListBoxItem; TextBlock t = GetNewsContent(item); t.Visibility = Visibility.Visible; } public T GetChild(DependencyObject obj) where T : DependencyObject { DependencyObject child = null; for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { child = VisualTreeHelper.GetChild(obj, i); if (child != null && child.GetType() == typeof(T)) { break; } else if (child != null) { child = GetChild(child); if (child != null && child.GetType() == typeof(T)) { break; } } } return child as T; } public T GetNewsContent(DependencyObject obj) where T : DependencyObject { DependencyObject child = null; for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) { child = VisualTreeHelper.GetChild(obj, i); if (child != null && child.GetType() == typeof(TextBlock) && ((TextBlock)child).Name == "newsContent") { break; } else if (child != null) { child = GetNewsContent(child); if (child != null && child.GetType() == typeof(TextBlock) && ((TextBlock)child).Name == "newsContent") { break; } } } return child as T; }
My XAML with my DataTemplate
<ListBox x:Name="newsList" Background="#00353535" BorderThickness="0,0,0,0" SelectionChanged="list_SelectionChanged" ItemContainerStyle="{StaticResource newsStyle}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Vertical"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path=Title}" x:Name="newsTitle"/> <TextBlock Width="80" Text="{Path=Date}" x:Name="newsDate"/> </StackPanel> <TextBlock x:Name="newsContent" Text="{Binding Path=Content}" Visibility="Collapsed"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate></ListBox>
I hope you could help me :)
Thanks for your attention
05-25-2009 7:00 AM |
... I just found the solution... I was blocked by this problem since Friday...
To solve this problem, I changed the AddedItems opacity to 1... But if someone can explain to me why I have to change the opacity, I didn't change it before... I just passed the visibility from collapsed to visible and from visible to collapsed...
Have a nice day :)