![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
| |
| | #1 (permalink) |
| | Problem when generating Tab items via binding to groups. I think that I may have stumbled across a bug, but it's possible that I'm doing something wrong. I set the ItemsSource of a TabControl to the Groups property of a CollectionViewSource. When I select a tab item, an exception is thrown stating that an invalid cast occurred when casting a TabItem to a GroupItem. I checked this out in Reflector and noticed that the ItemContainerGenerator.GetRealizedItemBlockCount method is using a C-style cast, making the assumption that ItemContainerGenerator.RealizedItemBlock.ContainerAt(int) is going to return a GroupItem. I don't see why this assumption should be made. Is there some overriding reason for not allowing TabItems to be the container? Here's the test app (there is no code-behind): <Window x:Class="TabbedGroups.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="TabbedGroups" Height="300" Width="300" > <Window.Resources> <XmlDataProvider x:Key="cities"> <x:XData> <Cities xmlns=""> <City CityName="Shanghai" Country="People's Republic of China" Continent="Asia"/> <City CityName="Tokyo" Country="Japan" Continent="Asia"/> <City CityName="Sydney" Country="Australia" Continent="Australia"/> <City CityName="Munich" Country="Germany" Continent="Europe" /> <City CityName="Stockholm" Country="Sweden" Continent="Europe" /> <City CityName="Chicago" Country="United States" Continent="North America" /> <City CityName="Denver" Country="United States" Continent="North America"/> <City CityName="Los Angeles" Country="United States" Continent="North America"/> <City CityName="Buenos Aires" Country="Argentina" Continent="South America" /> </Cities> </x:XData> </XmlDataProvider> <CollectionViewSource x:Key="citiesView" Source="{Binding Source={StaticResource cities}, XPath=Cities/City}"> <CollectionViewSource.GroupDescriptions> <PropertyGroupDescription PropertyName="@Country" /> </CollectionViewSource.GroupDescriptions> </CollectionViewSource> <DataTemplate x:Key="cityListItemTemplate"> <TextBlock Text="{Binding XPath=@CityName}" Margin="2,4"/> </DataTemplate> <DataTemplate x:Key="cityListTemplate"> <!-- NOTE: This ListBox can be removed and the exception is still thrown. --> <ListBox DataContext="{Binding Path=Items}" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" ItemTemplate="{StaticResource cityListItemTemplate}"/> </DataTemplate> </Window.Resources> <Grid DataContext="{StaticResource citiesView}"> <TabControl x:Name="tabControl" ContentTemplate="{StaticResource cityListTemplate}" ItemsSource="{Binding Path=Groups}" /> </Grid> </Window> ***************************************** Here is part of the exception that is thrown when clicking on a tab header: {System.InvalidCastException: Unable to cast object of type 'System.Windows.Controls.TabItem' to type 'System.Windows.Controls.GroupItem'. at System.Windows.Controls.ItemContainerGenerator.GetRealizedItemBlockCount(RealizedItemBlock rib, Int32 end) at System.Windows.Controls.ItemContainerGenerator.DoLinearSearch(DependencyObject& container, Object& item, Int32& itemIndex) at System.Windows.Controls.ItemContainerGenerator.ContainerFromItem(Object item) at System.Windows.Controls.Primitives.Selector.ItemSetIsSelected(Object item, Boolean value) at System.Windows.Controls.Primitives.Selector.SelectionChanger.CreateDeltaSelectionChange(List`1 unselectedItems, List`1 selectedItems) at System.Windows.Controls.Primitives.Selector.SelectionChanger.End() at System.Windows.Controls.Primitives.Selector.SetSelectedHelper(Object item, FrameworkElement UI, Boolean selected) at System.Windows.Controls.Primitives.Selector.NotifyIsSelectedChanged(FrameworkElement container, Boolean selected, RoutedEventArgs e) at System.Windows.Controls.Primitives.Selector.OnSelected(Object sender, RoutedEventArgs e) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(RoutedEventArgs args) at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e) at System.Windows.Controls.TabItem.OnSelected(RoutedEventArgs e) at System.Windows.Controls.TabItem.OnIsSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) ... ***************************************** Any help or suggestions about this issue would be much appreciated. Thank you, Josh Smith |
My System Specs![]() |
| | #2 (permalink) |
| | RE: Problem when generating Tab items via binding to groups. I've been playing around with this more, and found another manifestation of the same problem. I bound a TreeView's ItemsSource to a ListCollectionView's Groups property, and set the ItemTemplate to this template: <HierarchicalDataTemplate x:Key="hierarchicalItemTemplate" ItemsSource="{Binding Path=Items}"> <TextBlock Text="{Binding Converter={StaticResource converter}}" Margin="4,2" /> </HierarchicalDataTemplate> When navigating the tree items with the Up and Down arrow keys, the same exception is thrown as listed in my previous post. If you need my complete source code to test this, please let me know and I'd be happy to post it. Thanks, Josh "Josh Smith" wrote: > I think that I may have stumbled across a bug, but it's possible that I'm > doing something wrong. I set the ItemsSource of a TabControl to the Groups > property of a CollectionViewSource. When I select a tab item, an exception > is thrown stating that an invalid cast occurred when casting a TabItem to a > GroupItem. I checked this out in Reflector and noticed that the > ItemContainerGenerator.GetRealizedItemBlockCount method is using a C-style > cast, making the assumption that > ItemContainerGenerator.RealizedItemBlock.ContainerAt(int) is going to return > a GroupItem. I don't see why this assumption should be made. Is there some > overriding reason for not allowing TabItems to be the container? > > Here's the test app (there is no code-behind): > > <Window x:Class="TabbedGroups.Window1" > xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" > xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > Title="TabbedGroups" Height="300" Width="300" > > > <Window.Resources> > <XmlDataProvider x:Key="cities"> > <x:XData> > <Cities xmlns=""> > <City CityName="Shanghai" Country="People's Republic of China" > Continent="Asia"/> > <City CityName="Tokyo" Country="Japan" > Continent="Asia"/> > <City CityName="Sydney" Country="Australia" > Continent="Australia"/> > <City CityName="Munich" Country="Germany" > Continent="Europe" /> > <City CityName="Stockholm" Country="Sweden" > Continent="Europe" /> > <City CityName="Chicago" Country="United States" > Continent="North America" /> > <City CityName="Denver" Country="United States" > Continent="North America"/> > <City CityName="Los Angeles" Country="United States" > Continent="North America"/> > <City CityName="Buenos Aires" Country="Argentina" > Continent="South America" /> > </Cities> > </x:XData> > </XmlDataProvider> > > <CollectionViewSource x:Key="citiesView" Source="{Binding > Source={StaticResource cities}, XPath=Cities/City}"> > <CollectionViewSource.GroupDescriptions> > <PropertyGroupDescription PropertyName="@Country" /> > </CollectionViewSource.GroupDescriptions> > </CollectionViewSource> > > <DataTemplate x:Key="cityListItemTemplate"> > <TextBlock Text="{Binding XPath=@CityName}" Margin="2,4"/> > </DataTemplate> > > <DataTemplate x:Key="cityListTemplate"> > <!-- NOTE: This ListBox can be removed and the exception is still thrown. --> > <ListBox > DataContext="{Binding Path=Items}" > IsSynchronizedWithCurrentItem="True" > ItemsSource="{Binding}" > ItemTemplate="{StaticResource cityListItemTemplate}"/> > </DataTemplate> > > </Window.Resources> > > <Grid DataContext="{StaticResource citiesView}"> > > <TabControl > x:Name="tabControl" > ContentTemplate="{StaticResource cityListTemplate}" > ItemsSource="{Binding Path=Groups}" /> > > </Grid> > </Window> > ***************************************** > Here is part of the exception that is thrown when clicking on a tab header: > > {System.InvalidCastException: Unable to cast object of type > 'System.Windows.Controls.TabItem' to type 'System.Windows.Controls.GroupItem'. > at > System.Windows.Controls.ItemContainerGenerator.GetRealizedItemBlockCount(RealizedItemBlock rib, Int32 end) > at > System.Windows.Controls.ItemContainerGenerator.DoLinearSearch(DependencyObject& container, Object& item, Int32& itemIndex) > at > System.Windows.Controls.ItemContainerGenerator.ContainerFromItem(Object item) > at System.Windows.Controls.Primitives.Selector.ItemSetIsSelected(Object > item, Boolean value) > at > System.Windows.Controls.Primitives.Selector.SelectionChanger.CreateDeltaSelectionChange(List`1 unselectedItems, List`1 selectedItems) > at System.Windows.Controls.Primitives.Selector.SelectionChanger.End() > at System.Windows.Controls.Primitives.Selector.SetSelectedHelper(Object > item, FrameworkElement UI, Boolean selected) > at > System.Windows.Controls.Primitives.Selector.NotifyIsSelectedChanged(FrameworkElement container, Boolean selected, RoutedEventArgs e) > at System.Windows.Controls.Primitives.Selector.OnSelected(Object sender, > RoutedEventArgs e) > at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, > RoutedEventArgs routedEventArgs) > at System.Windows.EventRoute.InvokeHandlersImpl(Object source, > RoutedEventArgs args, Boolean reRaised) > at System.Windows.UIElement.RaiseEventImpl(RoutedEventArgs args) > at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e) > at System.Windows.Controls.TabItem.OnSelected(RoutedEventArgs e) > at System.Windows.Controls.TabItem.OnIsSelectedChanged(DependencyObject > d, DependencyPropertyChangedEventArgs e) ... > > ***************************************** > Any help or suggestions about this issue would be much appreciated. > > Thank you, > Josh Smith |
My System Specs![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Problem with generating a certificate | Network & Sharing | |||
| Thumbnail problem. Computer not generating them or something. | Vista music pictures video | |||
| PROBLEM WITH VS 2008 WINFORMS LINQ TREATED FOR GENERATING A INSTALLER | .NET General | |||
| SQLMetal - Problem generating VB from DBML | .NET General | |||
| Moving Start Groups lose your items | Vista security | |||