Recalling data from a listbox using code RRS feed

  • Question

  • Hello,

    I'm making a program in WPF that will load data from a database and populate a listbox using a data template.

    I have all the connections working and the data displaying in the listbox how i want it. (A sample of it is below).

    1<ListBox Margin="165.539,188,21.435,92.213" Name="lbUsers" Background="Transparent" Foreground="#FF3ACCA1" 
    2                     ItemTemplate="{DynamicResource UserListTemplate}" BorderBrush="Transparent" ItemContainerStyle="{StaticResource AdminItemContainerStyle}" SelectionMode="Single" IsSynchronizedWithCurrentItem="True"
    3                <ListBox.Resources> 
    4                    <!-- Data template for the listbox items --> 
    5                    <DataTemplate x:Key="UserListTemplate"
    7                        <Grid> 
    8                            <Grid.ColumnDefinitions> 
    9                                <ColumnDefinition Width="190*" /> 
    10                                <ColumnDefinition Width="400*" /> 
    11                                <ColumnDefinition Width="200*" /> 
    12                            </Grid.ColumnDefinitions> 
    14                            <StackPanel Orientation="Horizontal"
    16                                <StackPanel Orientation="Vertical" Margin="5"
    17                                    <TextBlock Text="{Binding Path=userUsername}" FontWeight="Bold" FontSize="14"  Foreground="#FF3ACCA1"/> 
    18                                    <TextBlock Text="{Binding Path=userFirstName}" Foreground="#FF3ACCA1"/> 
    19                                    <TextBlock Margin="10,0,0,0" Text="{Binding Path=userSurname}"  Foreground="#FF3ACCA1"/> 
    20                                </StackPanel> 
    21                            </StackPanel> 
    23                            </Grid> 
    24                        <!-- </Border> --> 
    25                    </DataTemplate> 
    26                </ListBox.Resources> 
    27            </ListBox> 

    I have created a button for each item in the list box.
    This is where my problem comes in:
    I cannot figure out how I can recall the data from the list box of the selected item to use elsewhere.

    So, for example, when the user clicks on the button on an item with index 2, the code jumps to the click event handler.
    In the handler I would like to be able to recall certain pieces of data from the listbox items dataset. (In this case, the username).

    Could anyone suggest how i can do this please? Thanks

    (Using C#)
    Friday, March 27, 2009 1:48 AM

All replies

  • Hi,

    I have similar problem too. This is what I did. 

     Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)

                Dim butn As Button = CType(e.OriginalSource, Button)
                Dim myObject As SomeObject = CType(butn.DataContext, SomeObject)

     End Sub

    myObject will be the instance of the object at that row.

    It works for me, so I hope it will work for you too.

    Thursday, April 2, 2009 6:33 AM