how to get the selected object in a listbox ---wpf - wpf

i have three listboxes, whose itemsSource is binded to a list, list and dictionary. i want to add the selected user and selected book to the dictionary with a command but i can't take the selected items. i am trying to obey the mvvm. i have a booklist and a userlist in my viewmodel which are binded to the given listboxes in my view. i couldn't send the selected items to my viewmodel. how can i do this?
thanks for helping in advance.

Just bind a property to SelectedItem:
<ListBox
ItemsSource="{Binding Books}"
SelectedItem="{Binding SelectedBook}"/>
And in the ViewModel
public class Library : INotifyPropertyChanged
{
public ObservableCollection<Book> Books {get;private set;}
public Book SelectedBook {get;set;}
}

In your viewModel, couldn't you create a SelectedBook and a SelectedUser and bind those to the SelectedItem of your ListBoxes? Then, when they change, add them to your dictionary.

You can also use standard binding syntax using a slash (/).
{Binding Books/}
will allow you to bind directly to the current item in a collection.

Related

MVVM Light and combobox

I am new to WPF and MVVM Light, I would appreciate if you could help me :-)
I would like to know how to implement a combobox with MVVM Light to do the following:
1) Select an item in the combobox
2) Based on the value selected, change other text fields in the GUI.
Thank you for your help.
Romain
Well:
View:
<ComboBox ItemsSource="{Binding SourceData}" SelectedItem="{Binding SelectedSourceData,Mode=TwoWay}"/>
<TextBlock Text="{Binding SelectedDataInTextFormat}"/>
ViewModel:
public class ViewModel:ViewModelBase
{
public ObservableCollection<Foo> SourceData{get;set;}
public Foo SelectedSourceData
{
get{return _selectedFoo;}
set{_selectedFoo=value;
RaisePropertyChanged("SelectedSourceData");
SelectedDataInTextFormat=Foo.ToString();
}
public string SelectedDataInTextFormat
{
get{return _selectedDataInTextFormat;}
set{_selectedDataInTextFormat=value;
RaisePropertyChanged("SelectedDataInTextFormat");
}
}
Basically, to ensure that your view model is able to receive the updated selected item from the combobox make sure the SelectedItem binding is set to Mode=TwoWay. To ensure that you're pushing data from the viewmodel to the view when a change occuers in the viewmodel make sure you call the RaisePropertyChanged helper class for the property you want updated in the view.

Bind whole element to property in class

I have a combobox in my XAML:
<ComboBox Name="MyComboBox" />
I want to bind the whole object to a property in my view class:
public class MyView : INotifyPropertyChanged
{
public ComboBox MyComboBox;
...
}
I know how to bind individual properties of the combobox to a property in my view class but is there a way to bind the whole element? I only need two properties so I could just bind the two properties separately but wanted to know if there is a way to do it in one shot.

Switching a binding with a binding

I have a BlogStore class which contains two observablecollections like so
public class BlogStore {
public ObservableCollection<Blog> blogs ...
public ObservableCollection<Blog> favourites ...
}
public BlogStore blogStore ...
no I want to reuse a control which does the following binding
ItemsSource="{Binding blogStore.blogs}
so that I can switch to favourites, the following does not work, but I would like something in a similar vein.
ItemsSource={Binding blogStore{Binding category, ElementName=blogControl}
and in the controls code behind i would have a dependency property.
maybe a converter could do the trick?
If you treat BlogStore as a ViewModel then it would expose a couple of other properties.
Category to which you bind what ever control you are using to choose the category to display.
Also a CategoryBlogs property which returns either the value of blogs or favourites depending on the value of Category.
You would be implementing INotifyPropertyChanged so you would ensure that a PropertyChanged event is fired for "CategoryBlogs" when the Category property is changed.
You would be binding ItemsSource just to CategoryBlogs.

WPF User Control Binding Issue?

I have this:
public MyView: UserControl
{
public IList<Person> PersonList { get; set; }
public MyView()
{
//code
}
public void Display(MyData myData)
{
DataContext=myData;
}
//code
}
The XAML for this includes a ComboBox :
ItemsSource="{Binding RelativeSource={RelativeSource Self}, Path=PersonList}"
For some reason this does not work and the combo box does not get populated ( however, If I use the code-behind and I say comboBox.ItemsSource = PersonList then the combo box does got populated ).
Any ideas ?
Regards,
MadSeb
Your property is set to private, and are you sure that you are setting the DataContext.
* EDIT *
Based on the change you made above, you're setting your datacontext incorrectly. Your "PersonList" is anIList<> on your MyView class, but you're setting your data context to something else.
Try adding items to PersonList within MyView and setting this.DataContext = this; Also, as suggested, switch your IList<> to an ObservableCollection<>.
I would also highly suggest reading up on the Model View ViewModel (MVVM) approach. It will help out a lot. Josh Smith has a lot of good articles about the MVVM approach (and has written a good book about it too).
Here's a link to his blog. His book is linked there, as well.
I suspect it's because you're not firing any property-changed events. If you don't notify your UI when the property's value is first set, the binding won't update. Look into the INotifyPropertyChanged interface and implement it in your class.
Similarly, if your IList property isn't an ObservableCollection or doesn't implement INotifyCollectionChanged, then when you add items to the list the databound UI won't reflect this.
I believe your binding statement is the problem.
"{Binding RelativeSource={RelativeSource Self}, Path=PersonList}" is looking for a "PersonList" on the combobox itself.
Are you seeing any binding errors in the output window?
Ideally you'd want to bind to a property in your DataContext (MyData)

How do I pass a specific viewmodel object in a button's CommandParam?

I have a simple WPF program using the Master-Detail UI pattern, where the Detail shows the currently selected item of a collection in the Master pane. I'm using MVVM, and each XAML page is backed by a ViewModel object which is set as it's DataContext.
Now, I want to add a DELETE button in the Master pane to delete from the master list of items. However, I can't figure out how to pass the viewmodel object of the currently selected item as a button CommandParameter to the routed command handler code.
Thanks in advance for any pointers.
Mike
Something similiar to what Paul has shown is where your view model will know which item is currently selected. I.e.
public class MyVM
{
public ObservableCollection<MyObject> MyCollection { get; set; }
public MyObject CurrentItem { get; set; }
}
Your XAML can then be simply
CommandParameter="{Binding Path=CurrentItem}"
As long as your master pane sets the CurrentItem property when you select it, your command can simple have the CurrentItem set as the command parameter.
One option would be to create each command with a reference to the view model, and create a property on the view model which is bound to the currently selected item. This way you don't need to pass the selected item as a parameter - the command can retrieve it from the VM. If this isn't suitable for your circumstances, you could pass the selected item something like this:
<Button Content="Delete"
Command="{Binding DeleteCommand}"
CommandParameter="{Binding ElementName=listBox_, Path=SelectedValue}" />
Where listBox_ is a control which derives from Selector.
Hope that helps,
Paul

Resources