WPF - ListBox Items collection being emptied
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: WPF - ListBox Items collection being emptied

Threaded View

  1. #1
    Join Date
    Oct 2004
    Posts
    206

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

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center