0

All is in the title, I want to do something like :

<ListBox AssignTo="{Binding ListBoxControl}" ... 

Then in my ViewModel having a property like this :

public ListBox CurrentListBox { get; set; } 

What I want is to easely retrieve the selected items

Could I do that?

5
  • 2
    If you are using MVVM pattern your ViewModel doesn't know about your View, so you shouldn't have View related properties there, like in this case ListBox. Your VM should only expose Model related properties which you can bind to your View, from what I understand. Commented Jun 14, 2013 at 13:55
  • 2
    view model shouldn't operate on controls Commented Jun 14, 2013 at 13:55
  • Could you do what? You have written a binding that binds to ListBoxControl and then don't define ListBoxControl anywhere. If you meant to bind to CurrentListBox then most likely what you want to do is use a ContentControl instead: <ConentControl Content="{Binding CurrentListBox}" />, but I'd recommend against it. Commented Jun 14, 2013 at 13:55
  • I want to change a gui used for launching integration test. I have currently a binding on SelectedItem, but I want to be able to select several at the same time. I don't want to make the code enven more unreadable that he is right now doing something like : here Commented Jun 14, 2013 at 14:01
  • Why don't you wrap the items in your collection in a container that has an isSelected property and then bind to that? Commented Jun 14, 2013 at 14:16

2 Answers 2

1

your xaml should looks like

 <ListBox ItemsSource="{Binding List}" SelectedItem="{Binding Item}"/> 

and your properties should looks something like

 private List<yourClass> list; public List<yourClass> List { get { return list; } set { list = value; RaisPropertyChanged("List"); } } private yourClass item; public yourClass Item { get { return item; } set { item = value; RaisPropertyChanged("Item"); } } 

now you only need to set your ViewModle as DataContext and there are many many way's to do this

i'm use a very simple way to do this i create an ResourceDictionary (VMViews.xaml)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:ProjectName.namespace" xmlns:vw="clr-namespace:ProjectName.namespace" > <DataTemplate DataType="{x:Type vm:YourVMName}"> <vw:YourVName/> </DataTemplate> </ResourceDictionary> 

and in my App.xaml

<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Path/VMViews.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> 

EDIT for SelectedItems

(this code isn't tested)

XAML

<ListBox ItemsSource="{Binding List}" SelectedItem="{Binding Item}" SelectionChanged="ListBox_SelectionChanged"> <ListBox.Resources> <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="IsSelected" Value="{Binding IsSelected}" /> </Style> </ListBox.Resources> </ListBox> 

now your the yourClass need to implement the IsSelected property

than you can do something like

private yourClass item; public yourClass Item { get { return item; } set { item = value; RaisPropertyChanged("Item"); SelectedItems = List.Where(listItem => listItem.IsSelected).ToList(); } } private List<yourClass> selectedItems; public List<yourClass> SelectedItems { get { return selectedItems; } set { selectedItems = value; RaisPropertyChanged("SelectedItems"); } } 
Sign up to request clarification or add additional context in comments.

7 Comments

oops... you have "vaule", not "value" :)
I want multiple selected Items, not only one ! SelectedItems="{Binding SelectedTest}" SelectionMode="Extended"/>
One is working fine by the way, why Multiple is such a pain?
oh sry @Thomas i thought only one so you need an behavior because you can't bind directly to SelectedItems AFAIK
@Thomas see my Edit maybe that will help you it is based on THIS and if it doesn't work take a look at THIS link
|
0

Another person had similar issues while trying to learn...

Take a look at this answer to help

Your property is exposed via public getter/setter (although setter can be private to your data context object).

Then, the property of the listbox "Items" will get "bound" to your exposed property.

4 Comments

I need access to selectedItems, when I tried to use classic binding I have :'SelectedItems' property is read-only and cannot be set from markup.
@Thomas, then I would expand the properties on your model as WiiMaxx has sampled, but instead of a single "Item", you want a list of items to handle multiple item selections... but the principle is still the same.
I am sorry but after reading 5 time the post you cite and your answer, I don't see the link with how to retrieve items selected by the user.
@Thomas, click on the hyperlink text of "Take a look at this answer to help"... the coloring is a poor choice and almost looks identical to normal text.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.