开发者

accessing an item variable from dynamic data in listbox on SelectionChanged

开发者 https://www.devze.com 2023-03-23 02:36 出处:网络
I am populating a listbox from a webcclient, the data is binded to the listbox and not saved anywhere..

I am populating a listbox from a webcclient, the data is binded to the listbox and not saved anywhere..

Id like to access binded information when the user selects from the listbox im having trouble accessing the value of a text block from the SelectionChanged event..

            <ListBox x:Name="UsersListBox" ItemsSource="{Binding Items}" Height="471" VerticalAlignment="Top" HorizontalAlignment="Left" Width="457" SelectionChanged="TargetsListBox_SelectionChanged" Grid.ColumnSpan="2">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="0,0,0,17" Width="432" Orientation="Horizontal">
                        <Image x:Name="ImageAddIcon" Source="blkAdd.png" Height="60" Width="71" VerticalAlignment="Stretch" />
                        <Image x:Name="ImagePointer" Source="blkClick.png" Height="60" Width="71" VerticalAlignment="Stretch" />
                        <StackPanel>
                            <TextBlock Name="txtID" Text="{Binding PlayerID}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" Foreground="#FF8A9A8A" Visibility="Collapsed" />
                            <TextBlock Name="txtNick" Text="{Binding Nickname}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" Foreground="#FF8A9A8A" />
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
    开发者_Go百科    </ListBox>

Id like to access the PlayerID from this dynamicly populated list(from webservice) on a onselected basis

private void TargetsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  string  ID  = UsersListBox.SelectedItem ???PlayerID or txtID???;
}

i just want to get the player id that is binded to the listbox with a selection changed event ANY IDEAS!!!!!!!!! <3


Assuming that "Items" is a ObservableCollection:

private void TargetsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var listBox = sender as ListBox;

    var selectedItem = listBox.SelectedItem as Player;
    if (selectedItem != null)
    {
        string id = selectedItem.PlayerID
        string nick = selectedItem.NickName;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消