Unhandled Error in Silverlight Application Code: 4004 System.Collections.Generic.KeyNotFoundException - silverlight-4.0

I'm getting this error in a very strange manner. I've been able to isolate it, and I know what is causing it, but I don't have a clue why.
This is the situation: I have a ChildWindow, which contains a TabControl, which contains two UserControl, and both of them contain a datagrid like this causing the unhandled error:
<sdk:DataGrid x:Name="PersonEmailDataContainer" AutoGenerateColumns="False" Height="119" HorizontalAlignment="Left" Margin="12,39,0,0" VerticalAlignment="Top" Width="736"
ItemsSource="{Binding PagedListOfPersonEmail, Mode=TwoWay}"
ColumnHeaderStyle="{StaticResource ColBinding}"
SelectedItem="{Binding SelectedPersonEmail, Mode=TwoWay}"
IsReadOnly="{Binding PersonEmailDataContainerIsReadOnly, Mode=TwoWay}">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn CanUserReorder="True" CanUserResize="True"
CanUserSort="True" Width="Auto" />
<sdk:DataGridTextColumn CanUserReorder="True" CanUserResize="True"
CanUserSort="True" Width="20*"
Binding="{Binding Email, Mode=TwoWay}"
Header="{Binding ConverterParameter=HdrEmail, Converter={StaticResource Localization}, Source={StaticResource Localization}}" />
<sdk:DataGridTextColumn CanUserReorder="True" CanUserResize="True"
CanUserSort="True" Width="20*"
Binding="{Binding WebSite, Mode=TwoWay}"
Header="{Binding ConverterParameter=HdrWebSite, Converter={StaticResource Localization}, Source={StaticResource Localization}}" />
</sdk:DataGrid.Columns>
<!-- more columns -->
</sdk:DataGrid>
I solved it by removing the first column, which is just a blank column. It displays no data, it has no bindings, it doesn't even have a header to display. Any ideas why this was causing the error?

If you have a DataGrid that is bound to an ItemsSource then you cannot have a sdk:DataGridTextColumn that does not have a binding. You can use the sdk:DataGridTemplateColumn instead. This column type does not require a binding.

Related

Getting values from a keyvaluepair in xaml

I have a GridView that's bound to a Dictionary:
<GridView x:Uid="SearchInputRecentSearches"
x:Name="SearchInputRecentSearches"
ItemsSource="{Binding RecentSearches}"
Grid.Row="1"
Grid.Column="1"
Style="{StaticResource RecentSearchGrid}">
Inside of this GridView, in the ItemTemplate, I want to show the Key or Value:
<GridView.ItemTemplate>
<DataTemplate>
<StackPanel Style="{StaticResource RecentSearchesItemStackPanel}">
<TextBlock Text="{Binding Key}"/>
<Button Width="31"
Height="31"
Command="{Binding DataContext.ExecuteDeleteSearch, ElementName=SearchInputRecentSearches}"
CommandParameter="{Binding Key}"
Template="{StaticResource DeleteButton}"/>
<TextBlock Style="{StaticResource RecentSearchStyle}"
Text="{Binding Value}" />
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
However, nothing displays for either Key or Value when I run this. I've tried referring to Path=Key and Path=Value inside of the binding, but this doesn't work, either. I'm currently using a ValueConverter to get the key or value out, but this seems a little unnecessary and I was hoping someone could point out what I was doing wrong!

Invalid XAML suddenly in DataTemplate for longlistselector items (WP8)

Had this error a few days ago and it seemed to go away. Now it wont go away. I am also getting an Adcontrol error: ApplicationId and AdUnitId need to be set before using this control Adcontrol is set up the exact same way in another app so I don't see what the issue is and cant find any information on it.
EDIT:
If I remove the portion of the code with double brackets }}, the Invalid Error goes away. I removed the Adcontrol all together to isolate the error as well. However, when I run the app, I still get this:
**A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll
Additional information: Could not load file or assembly 'Microsoft.Phone.Controls.Toolkit.resources, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.**
I have uninstalled and reinstalled WPToolkit. Im using similar code and WPToolkit in another app and it does not give me this error. What else can I do?
INVALID XAML:
<DataTemplate x:Key="SoundTileDataTemplate">
<StackPanel>
<Grid Margin="0,5,6,0" Height="100" Width="140"
toolkit:TiltEffect.IsTiltEnabled="True">
<Border BorderBrush="#FF49A609" BorderThickness="1" CornerRadius="3,3,3,3" Background="{StaticResource PhoneAccentBrush}" Opacity=".6"/>
<TextBlock Text="{Binding Title}" FontSize="19" TextWrapping="Wrap" Width="140" FontFamily="/BBSM;component/Fonts/123Sketch.ttf#123Sketch" TextAlignment="Center" />
<Image Source="/Assets/tiles/TRXHSBRGIcon.png" Width="30" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,0,6,6" Visibility="{Binding Status, Converter={StaticResource DownloadStatusToIconVisibilityConverter}}" />
<ProgressBar Height="12" VerticalAlignment="Bottom" Padding="0" Margin="0" Foreground="{StaticResource PhoneForegroundBrush}" Value="{Binding DownloadProgress}" Visibility="{Binding Status, Converter={StaticResource DownloadStatusToProgressBarVisibilityConverter}}"/>
</Grid>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="ExtrasTileDataTemplate">
<StackPanel>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu IsZoomEnabled="False" >
<toolkit:MenuItem Header="Save as Ringtone" Command="{Binding SaveSoundAsRingtone}" CommandParameter="{Binding FilePath}" />
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
<Grid Margin="0,5,6,0" Height="100" Width="140"
toolkit:TiltEffect.IsTiltEnabled="True">
<Border BorderBrush="#FF49A609" BorderThickness="1" CornerRadius="3,3,3,3" Background="{StaticResource PhoneAccentBrush}" Opacity=".6"/>
<TextBlock Text="{Binding Title}" FontSize="19" TextWrapping="Wrap" Width="140" FontFamily="/BBSM;component/Fonts/123Sketch.ttf#123Sketch" TextAlignment="Center" />
<Image Source="/Assets/tiles/TRXHSBRGIcon.png" Width="30" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,0,6,6" Visibility="{Binding Status, Converter={StaticResource DownloadStatusToIconVisibilityConverter}}" />
<ProgressBar Height="12" VerticalAlignment="Bottom" Padding="0" Margin="0" Foreground="{StaticResource PhoneForegroundBrush}" Value="{Binding DownloadProgress}" Visibility="{Binding Status, Converter={StaticResource DownloadStatusToProgressBarVisibilityConverter}}"/>
</Grid>
</StackPanel>
</DataTemplate>
Adcontrol:
<UI:AdControl ApplicationId="*********" AdUnitId="******" Width="480" IsAutoRefreshEnabled="True" Grid.Row="1" Height="80"/>
When I run the project I get the following. I continue after each one, and the app runs, but the Pivot Headers are all screwed up but app seems functional.
A first chance exception of type 'Microsoft.Advertising.Shared.AdException' occurred in Microsoft.Advertising.Mobile.DLL
Additional information: You can not use PubCenter IDs for testing in the emulator. If you want to test with these IDs, please deploy your application to a device. Otherwise please change your ApplicationId to "test_client" and AdUnitId to one of the supported ad types as outlined in the documentation.
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll
Additional information: Could not load file or assembly 'Microsoft.Phone.Controls.Toolkit.resources, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
if you are using windows phone toolkit then
you have to include
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
xmlns:cc="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
in your *.xaml page

Silverlight Datagrid Column header Text Break Line

I am Using Silverlght 4.I have a datagrid Header whose text="StudentDetailOfAplication".Since It Occupies more length in I need to have a break line near "Application".Please send me some usefull links how to do this
<Grid x:Name="LayoutRoot" Background="White">
<sdk:DataGrid AutoGenerateColumns="False" Height="227" HorizontalAlignment="Left" Margin="39,37,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="316" >
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="StudenDetailsOFApplication" Width="100" Binding="{Binding id}"></sdk:DataGridTextColumn>
<sdk:DataGridTextColumn Header="Name" Width="100" Binding="{Binding productName}"></sdk:DataGridTextColumn>
<sdk:DataGridTextColumn Header="NumberOfAplication" Width="114" Binding="{Binding qty}"></sdk:DataGridTextColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid></Grid>
Header="Student Details
Of Application" or Header="Student Details
Of Application" will also work. Or if you're not inserting into a string you can also do something like.
<TextBlock>
Line<LineBreak/>Break
</TextBlock>
but personally I just use the codes shown in the first two examples. Also keep in mind the xml will retain whitespace, so you can do like Header=" Details of
Student Application" for alignment purposes if TextAlignment property isnt available. Hope this helps.

Using different databinding sources within ListBox and ContextMenus

Here is the XAML:
<ListBox ItemsSource="{Binding Documents}" BorderBrush="{x:Null}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Title}" FontSize="12" FontWeight="Bold" />
<TextBlock Text="{Binding ID}" FontSize="10" FontStyle="Italic" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ContextMenu>
<ContextMenu ItemsSource="{Binding CategoryList}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<MenuItem Command="{Binding AddDocumentToCategoryContextMenuCommand}" Header="{Binding Category.Name}" />
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
Ok so the ListBox's ItemSource is bound to the Documents collection in the VM and properly renders the Titles and IDs
The Context Menu's ItemSource is bound to the CategoryList collection in the VM and properly renders the list of categories.
The problem I have is with the Command Binding:
Command="{Binding AddDocumentToCategoryContextMenuCommand}"
Since the ItemSource for the ContextMenu is already set, it tries to get the AddDocumentToCategoryContextMenuCommand from CategoryList. Obviously the command is not there, it is a member of the VM.
I do not want any references to the VMs or Models in the XAML. Everything is constructed using Unity and VM-View is associated in App.xaml:
<Application.Resources>
<DataTemplate DataType="{x:Type vms:FeedViewModel}">
<views:FeedView/>
</DataTemplate>
<DataTemplate DataType="{x:Type vms:DocumentsViewModel}">
<views:DocumentsView/>
</DataTemplate>
<DataTemplate DataType="{x:Type vms:ManagementViewModel}">
<views:ManagementView/>
</DataTemplate>
<DataTemplate DataType="{x:Type dev:DevelopmentViewModel}">
<dev:DevelopmentView />
</DataTemplate>
</Application.Resources>
How can I databind to a member of the VM from within the ContextItem.
Thanks.
UPDATED edit #1 starts Here
Here is the updated xaml (but still not working but some insight gained):
<ListBox ItemsSource="{Binding Documents}" x:Name="Results" BorderBrush="{x:Null}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Title}" FontSize="12" FontWeight="Bold" />
<TextBlock Text="{Binding ID}" FontSize="10" FontStyle="Italic" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ContextMenu>
<ContextMenu ItemsSource="{Binding CategoryList}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<MenuItem Command="{Binding ElementName=Results, Path=DataContext.AddDocumentToCategoryContextMenuCommand}" Header="{Binding Category.Name}" />
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
I have this example working for a simple example that does not use a ContextMenu. It appears that the ContextMenu (even though attached to the ListBox) is not part of the user control visual tree. The binding always comes back null / not found. I think the ContextMenu, because it is a floating "window" is constructed in its own tree and therefore cannot find the ListBox call "Results" in order to access the ListBox's DataContext.
Any thoughts on this? Any recommendations on how deal with?
Edit #2 Starts Here
In case you are are wondering, figured out the answer to the binding question:
This binding works:
Command="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.DataContext.AddDocumentToCategoryContextMenuCommand}
Hope this helps others with the same question.
One last update for completeness.
In order for the command to know which context menu item was clicked on, I had to change the xaml slightly (silly oversight):
<ListBox.ContextMenu>
<ContextMenu x:Name="Context" ItemsSource="{Binding CategoryList}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<MenuItem Command="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.DataContext.AddDocumentToCategoryContextMenuCommand}"
CommandParameter="{Binding Category.ID}"
Header="{Binding Category.Name}" />
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</ListBox.ContextMenu>
Again, hope this helps others.
Use the ElementName property of the binding to specify that. You'd end up with something like this:
Command="{Binding ElementName=ViewModelObject
Path=AddDocumentToCategoryContextMenuCommand}"

How to get control parent DataContext for element to element databinding?

Suppose I have a user control which datacontext is bound to a VM. This VM has a property for a list MyList.
Inside this user control I have a ComboBox, I want to set following kind of xaml
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel HorizontalAlignment="Stretch">
<sdk:DataGrid ItemsSource="{Binding YourList}" IsReadOnly="True" AutoGenerateColumns="False" >
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<!-- ...... -->
<sdk:DataGridTemplateColumn Header="User" >
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding ElementName=LayoutRoot, Path=DataContext.MyList}" DisplayMemberPath="Value" SelectedValuePath="Key" SelectedValue="{Binding UserID}" ></ComboBox>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</StackPanel>
</Grid>
but it is not working.
How to resolve this problem?
This worked form me. This was the ItemSource for a ComboBox that was within a DataGrid:
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=sdk:DataGrid},
Path=DataContext.Teams}">
Are you trying to get to the main VM from within the UserControl? take a look at this solution. http://weblogs.asp.net/dwahlin/archive/2009/08/20/creating-a-silverlight-datacontext-proxy-to-simplify-data-binding-in-nested-controls.aspx