CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2004

    Question WPF - ListBox Items collection being emptied


    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?



    This is the DataTemplate that I am using:

    <DataTemplate x:Key="NonEPOSCall">
    	<Grid Margin="0" Background="White">
    			Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=Background}" 
    			<Grid Margin="3" Style="{StaticResource GridStyle}">
    					<ColumnDefinition Width="Auto"></ColumnDefinition>
    					<ColumnDefinition Width="*"></ColumnDefinition>
    				<Grid Grid.Column="1" HorizontalAlignment="Left" ShowGridLines="False">
    						<ColumnDefinition SharedSizeGroup="FirstColumn"></ColumnDefinition>
    						<ColumnDefinition SharedSizeGroup="SecondColumn"></ColumnDefinition>
    					<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 Grid.Row="0" Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Center">
    						<TextBlock Margin="100,3,3,3">
    								Style="{StaticResource HyperlinkStyle}"
    								Next Call Date:
    						<TextBlock FontWeight="Bold" Tag="tbNextCallDate" Margin="20,3,3,3" Text="{Binding Path=NextCallDate}" VerticalAlignment="Center"></TextBlock>
    This is how I fill the listbox:

    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
    Attached Files Attached Files
    Last edited by dlarkin77; June 30th, 2008 at 09:11 AM.

  2. #2
    Join Date
    Sep 2009

    Re: WPF - ListBox Items collection being emptied


    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:


    <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" />
    <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"/>




    private void Hyperlink_Click(object sender, RoutedEventArgs e)
    Hyperlink localHyperLink = (Hyperlink)sender;

  3. #3
    Join Date
    Sep 2009

    Re: WPF - ListBox Items collection being emptied

    hi disc

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.