0

Im busy with my app and i walked in some problems when i click on a photo in my listbox PhotoFeed.

I got 1 List<> with in it the strings UrlTumb and UrlFull.

I got 1 ListBox with in it a WrapPanel filled with images wich i set the Image.Source from my UrlTumb.

What my problem is when i click on a photo in my listbox i want to navigate to a new page and display there the original image (UrlFull) now i can only get my UrlTumb from my Image.Source but i want my UrlFull which is stored in the List. Now is my question how do i obtain the UrlFull. So how can i back trace which item i clicked and get the UrlFull from that item so i can send it with my NavigationService.Navigate

I can do it on an dirty way and create an invisible textblock besides the image in my ListBox and put the UrlFull in there but i would like to do it in a proper way

So what do i place in the ????? spot in this line

            NavigationService.Navigate(new Uri("/PhotoInfo.xaml?urlfull={0}", ????? , UriKind.Relative));

Greetings Cn

MrME
  • 337
  • 2
  • 14

1 Answers1

2

There are multiple options:

  1. Use selected item's index listBox.SelectedIndex to get the index of the selected property which will correspond to the index in your source (it might not if you filter the collection using collection source, but I think that is not the case)
  2. Use selected item listBox.SelectedItem this will return the SelectedItem which will contain your object. (Note, that if your selection mode set to multiple, this will return only the firstly selected item)
  3. Use SelectemItems. It will allow you to get an array of selected
    items (Note: this should be normally used only when your list's
    selection mode is set to multiple)
  4. Use SelectedValue, which will contain the value of the SelectedItem (this will save you and extra step.
  5. Use arguments of the Selection changed event AddedItems.

Bellow is the code snippet of 3 options above. x, y, z will all be your selected names (e.g. "Mike")

XAML:

<ListBox x:Name="lb"
         ItemsSource="{Binding Names}"
         SelectionChanged="NameChanged" />

Code behind:

    public class Person
    {
        public string Name { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }

    private List<Person> people = new List<Person>
        {
            new Person{Name = "Lewis"},
            new Person{Name = "Peter"},
            new Person{Name = "Brian"}
        };

    public List<Person> People
    {
        get
        {
            return this.people;
        }
        set
        {
            this.people = value;
        }
    }

    private void NameChanged(object sender, SelectionChangedEventArgs e)
    {
        var x = this.people[lb.SelectedIndex];
        var y = lb.SelectedItem;
        var z = lb.SelectedItems[0];
        var h = lb.SelectedValue;

        var u = e.AddedItems[0];

        var person = e.AddedItems[0] as Person;
        if (person != null)
        {
            var result = person.Name;
        }
    }

For the differences between SelectedValue and SelectedItem refer here SelectedItem vs SelectedValue

Community
  • 1
  • 1
Vitalij
  • 4,587
  • 9
  • 42
  • 65
  • var person = e.AddedItems[0] as Person; if (person != null) { var result = person.Name; } Totally worked for me :D Ty – MrME Apr 02 '12 at 20:00