1

I want to get the SelectedItem from a ListBox which looks like this inside my Windows 8 Store App:

<ListBox x:Name="listBox" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" Foreground="Black" BorderThickness="0" Background="#FFD8D8D8" />

The problem is, that the ListBox don't fire the SelectedItem propertie. I have to use IsSynchronizedWithCurrentItem="True" but then an error appears which says the true isn't supportet for this property. What do I have to do or are there any other ways to get the SelectedItem propertie?

I have this Code behind:

namespace ExampleApp
{
    public sealed partial class MainPage : Page, INotifyPropertyChanged
    {
        private object currentItem;

        //Constructor and so on

        public object SelectedItem
        {
            get { Debug.WriteLine("get"); return currentItem; }
            set { Debug.WriteLine("set"); currentItem = value; NotifyPropertyChanged(); }
        }
    }
}
Cilenco
  • 6,951
  • 17
  • 72
  • 152
  • Possible duplicate of [How to pass listbox selecteditem as command parameter in a button?](http://stackoverflow.com/questions/18257516/how-to-pass-listbox-selecteditem-as-command-parameter-in-a-button) – RubberDuck Jun 10 '16 at 19:14

1 Answers1

4

you should try this

<ListBox x:Name="listBox" SelectedItem="{Binding ElementName=YourPageName,path=DataContext.SelectedItem, Mode=TwoWay}" Foreground="Black" BorderThickness="0" Background="#FFD8D8D8" />
Jignesh.Raj
  • 5,776
  • 4
  • 27
  • 56
  • Thank you it works great. I had to remove the DataContext bevor SelectedItem and now it works wunderful. – Cilenco May 01 '13 at 08:28
  • Wow. Thanks. I suppose there is a reason why Microsoft did not implement reasonable OneWayToSource binding on SelectedItem, but I can't think of one. – George Jan 21 '15 at 20:01