tags:

views:

413

answers:

1

Hi,

I have a Main window with textbox. I enter some text and click enter it popups a window with listbox and displays the items matching the text entered in the Mainwindow. On selecting the item from the ListBox the text gets set in my textbox of Mainwindow.

I am following the MVVM Pattern. I am not able to set the binding for my listbox in my Mainwindow.(Using CommandBinding)

Does anyone has some solution or sample for the similar scenario?

Thanks

A: 

On the ViewModel you need a property SelectedListBoxItem

Bind it to the SelectedItem of your ListBox.

Bind the Text-property to the SelectedListBoxItem of your ViewModel

and that should basically be it.

<ListBox
        ItemsSource="{Binding Path=ItemsView, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"    
        SelectedItem="{Binding Path=SelectedListBoxItem, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"
        />

<TextBox Text="{Binding Path=SelectedListBoxItem, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"
     />
Natrium
I was trying the something similar but it didn't worked for me. Can u Please paste the sample code.?