简体   繁体   中英

How to get value of selected listbox item

I'm loading data from an xml file into a listbox . Here is my xaml

<ListBox x:Name="lstSearchCategory" FontFamily="Arial Black" 
         VerticalAlignment="Center" Margin="25,69,19,10" Height="264">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel >
                <Image Source="{Binding Image}" Height="100" Width="100" 
                       HorizontalAlignment="Left"></Image>
                <TextBlock HorizontalAlignment="Right" Text="{Binding Name}" 
                           FontSize="30" Foreground="Black" Margin="140,-100,0,0"/>
                <TextBlock Text="{Binding Category}" FontSize="24" 
                           Foreground="Black" Margin="10,-10,0,0"/>
                <TextBlock Text="{Binding Price}" HorizontalAlignment="Right" 
                           Foreground="Red" Margin="300,-25,0,16"/>
                <Rectangle Width="500"  Fill="Black" Height="0.5"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

This is working fine. Now I want that when I select any listbox item, I get its respective values ie image, price, category etc. How can i do this ? Help

You need to get the selected item in a ListBox Event and get the DataTemplate from the ListBox (as seen on MSDN ):

private void lstEvents_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBoxItem lbi = (lstEvents.ItemContainerGenerator.ContainerFromIndex(lstEvents.SelectedIndex)) as ListBoxItem;
            ContentPresenter cp = GetFrameworkElementByName<ContentPresenter>(lbi);
            DataTemplate dt = lstEvents.ItemTemplate;
            Label l = (dt.FindName("lblEventId", cp)) as Label;
            MessageBox.Show(l.Content.ToString());   
        }

You need generate Tap = "lstSearchCategory_Tap" in your XAML file and below code in .cs file

private void lstSearchCategory_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    try
    {
        ListBox ListBoxSelecteditem = (ListBox)sender;
        YourModel model = (YourModel)ListBoxSelecteditem.SelectedItem;            

        string name = model.Name;
        string cat = model.Category;
        .......

        string ControlName = ((System.Windows.FrameworkElement)
                             (((System.Windows.RoutedEventArgs)(e)).OriginalSource)).Name;
        if (ControlName.ToLower() != "name".ToLower())
        {
        }
    }
    catch (Exception ex)
    { }
}   

try this

<ListBox Tap="lstSearchCategory_Tap" x:Name="lstSearchCategory">

and than on tap event add this

    var selected = (classname)lstSearchCategory.SelectedValue;
   MessegeBox.Show(selected.Name + selected.Price);

here class-name is name of class where you are binding the name, price etc values

If you fill your ListBox via binding, you should have some property lile SelectedItem in your view model. So the currently selected item should always be stored in the viewmodel for easy access. Just add a binding to SelectedItem in your viewmodel and every thing should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM