1 Attachment(s)
WPF - ListBox Items collection being emptied
Hi,
I have a user control that is essentially a listbox.
The listbox is bound to a list of objects.
I am using an ItemTemplateSelector to control how each item is displayed.
In one template I am using a Hyperlink.
My problem is as follows:
Whenever I click the hyperlink the listbox's items collection seems to be emptied out.
I can still see the listbox items but if I query myListBox.Items.Count I always get 0.
Is there a reason this should happen?
Thanks,
dlarkin77
This is the DataTemplate that I am using:
Code:
<DataTemplate x:Key="NonEPOSCall">
<Grid Margin="0" Background="White">
<Border
Margin="5"
BorderThickness="1"
BorderBrush="SteelBlue"
Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=Background}"
CornerRadius="4">
<Grid Margin="3" Style="{StaticResource GridStyle}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1" HorizontalAlignment="Left" ShowGridLines="False">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="FirstColumn"></ColumnDefinition>
<ColumnDefinition SharedSizeGroup="SecondColumn"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" Grid.Column="0" Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock Text="Account Number:" Margin="3"></TextBlock>
<TextBlock FontWeight="Bold" Margin="3" Text="{Binding Path=AccountNumber}"></TextBlock>
</StackPanel>
<StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock Margin="100,3,3,3">
<Hyperlink
Style="{StaticResource HyperlinkStyle}"
Click="Hyperlink_Click">
Next Call Date:
</Hyperlink>
</TextBlock>
<TextBlock FontWeight="Bold" Tag="tbNextCallDate" Margin="20,3,3,3" Text="{Binding Path=NextCallDate}" VerticalAlignment="Center"></TextBlock>
</StackPanel>
</Grid>
</Grid>
</Border>
</Grid>
</DataTemplate>
This is how I fill the listbox:
Code:
List<CallListItem> items = new List<CallListItem> {
new CallListItem { AccountNumber = "456", EPOS = false },
new CallListItem { AccountNumber = "789", EPOS = false }
};
// callListBox1 is the user control
// lstCalls is the name of the listbox in callListBox1
callListBox1.lstCalls.ItemsSource = items;
Edit: I've attached a sample application showing the behaviour inquestion. You'll need VS 2008 to open it
Re: WPF - ListBox Items collection being emptied
Hi,
Try setting the NavigateUri property of your Hyperlink. I ran into this same issue. As soon as I populated this property the object being passed to the event stopped being null.
Here is my code:
XAML:
<DataTemplate x:Key="appsTemplate">
<StackPanel >
<TextBlock Text="{Binding Path=applicant}" FontWeight="Bold" ToolTip="Name of Applicant"/>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Path=vchJobs}" FontStyle="Italic" ToolTip="Postion Title" Width="200"/>
<TextBlock ToolTip="View Application" Width="200" Hyperlink.RequestNavigate="hyperlink_RequestNavigate">
<Hyperlink NavigateUri="{Binding Path=URL}" Click="Hyperlink_Click" Name="Hyperlink" >
<TextBlock Text="View Application" />
</Hyperlink>
</TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal" >
<TextBlock Margin="0,0,0,0" Text="{Binding Path=vchDept}" HorizontalAlignment="Right" Width="110" ToolTip="Department of Postion"/>
<TextBlock Margin="10,0,0,0" Text="{Binding Path=location}" HorizontalAlignment="Right" Width="120" ToolTip="Location of Postion"/>
<TextBlock Margin="10,0,0,0" Text="{Binding Path=InsertDate}" HorizontalAlignment="Right" Width="70" ToolTip="Date Application Recieved"/>
<TextBlock Margin="10,0,0,0" Text="{Binding Path=vchAssigntoDesc}" HorizontalAlignment="Right" Width="100" ToolTip="Status assigned to application."/>
<TextBlock Margin="10,0,0,0" Text="{Binding Path=Letter}" HorizontalAlignment="Right" Width="20" ToolTip="Letter Sent"/>
</StackPanel>
</StackPanel>
</DataTemplate>
c#
private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
Hyperlink localHyperLink = (Hyperlink)sender;
System.Diagnostics.Process.Start(localHyperLink.NavigateUri.ToString());
}
Re: WPF - ListBox Items collection being emptied