Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6819243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:16:57+00:00 2026-05-26T21:16:57+00:00

I have a binding for ListBox but how I can add an event to

  • 0

I have a binding for ListBox but how I can add an event to selected item?

MainPage.xaml:

<!--ContentPanel - place additional content here-->
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <ListBox Name="list" SelectionChanged="list_SelectionChanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding ImageUri}" 
                           Stretch="None" 
                           Height="100"/>
                    <StackPanel Width="360" >
                        <TextBlock Text="{Binding Text}"
                                   Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                        <TextBlock Text="{Binding Opis}" 
                                   Style="{StaticResource PhoneTextSubtleStyle}"/>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

MainPage.xaml.cs:

public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();
        ObservableCollection<SampleData> dataSource = 
            new ObservableCollection<SampleData>();

        dataSource.Add(new SampleData() 
        { 
            ImageUri = "Images/appbar.delete.rest.png", 
            Text = "Item1", 
            Description = "blablabla" 
        });
        dataSource.Add(new SampleData() 
        { 
            ImageUri = "Images/appbar.delete.rest.png", 
            Text = "Item2", 
            Description = "blablabla" 
        });
        dataSource.Add(new SampleData()
        { 
            ImageUri = "Images/appbar.download.rest.png", 
            Text = "Item3", 
            Description = "blablabla" 
        });

        this.list.ItemsSource = dataSource;         
    }

    private void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (list.SelectedItem == null) return;
        //what next?
    }

    public class SampleData
    {
        public string Text { get; set; }
        public string Description { get; set; }
        public string ImageUri { get; set; }
    }
}

I want to click for example on Item2 and get to page Page2.xaml.

My project from VS2010: http://www.przeklej.pl/plik/wp7sampleproject6-7z-00368v7i196u

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:16:58+00:00Added an answer on May 26, 2026 at 9:16 pm

    If you are using an MVVM stack like MVVMlight this is quite simple as you simply create a Full Property in your view model for a Selected Item and then bind to it as you would with anything else. Using your code above for example I would do:

            /// <summary>
        /// The <see cref="SelectedListItem" /> property's name.
        /// </summary>
        public const string SelectedStickPropertyName = "SelectedListItem";
    
        private SampleData _selectedItem;
    
        /// <summary>
        /// Gets the SelectedStick property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public SampleData SelectedListItem
        {
            get
            {
                return _selectedItem;
            }
    
            set
            {
                if (_selectedItem == value || value == null)
                {
                    return;
                }
    
                var oldValue = _selectedItem;
                _selectedItem = value;
    
                // Update bindings, no broadcast
                RaisePropertyChanged(SelectedStickPropertyName);
            }
        }
    

    Then in your Listbox XAML just bind the Listbox Selected Item to the above property (provided you have set the data context property:

    <ListBox Name="list" SelectionChanged="list_SelectionChanged" SelectedItem="{Binding SelectedListItem, Mode=TwoWay}">
    

    That way you can query the Selected Item Property to find the value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a listbox defined in XAML as: <ListBox x:Name=directoryList MinHeight=100 Grid.Row=0 ItemsSource={Binding Path=SelectedDirectories}/>
I have the following XAML code: <ListBox ItemsSource={Binding Languages}> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <Image Source={Binding
I avoided to ask this question, but the ListBox's selected index can no be
I have an ObservableCollection of addresses that I am binding to a ListBox. Then
I have some control like <ListBox ItemsSource={Binding tests, UpdateSourceTrigger=PropertyChanged} ItemTemplate={StaticResource TestTemplate} /> When user
I have created a listbox control with following DataTemplate <DataTemplate x:Key=lb_Itemtemplate> <DockPanel> <TextBlock Text={Binding}
OK... This is confusing me...I have a way I can think of doing this...but
I have a UserControl (XAML below) that has a ListBox that I want to
I have made a CheckedListBox using the following: <ListBox x:Name=lst_checkBoxList ItemsSource={Binding}> <ListBox.ItemTemplate> <DataTemplate> <CheckBox
can someone give the C# equivalent of this xamle code? <ListBox Width=400 Margin=10 ItemsSource={Binding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.