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?

Thomas
  • 5,603
  • 5
  • 32
  • 48
  • 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. – Adolfo Perez Jun 14 '13 at 13:55
  • 2
    view model shouldn't operate on controls – dkozl Jun 14 '13 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: ``, but I'd recommend against it. – Dan Puzey Jun 14 '13 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](http://stackoverflow.com/questions/451748/wpf-m-v-vm-get-selected-items-from-a-listcollectionview) – Thomas Jun 14 '13 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? – jfin3204 Jun 14 '13 at 14:16

2 Answers2

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");
        }
    }
WiiMaxx
  • 5,322
  • 8
  • 51
  • 89
  • oops... you have "vaule", not "value" :) – DRapp Jun 14 '13 at 14:20
  • I want multiple selected Items, not only one ! `SelectedItems="{Binding SelectedTest}" SelectionMode="Extended"/>` – Thomas Jun 14 '13 at 14:21
  • One is working fine by the way, why `Multiple` is such a pain? – Thomas Jun 14 '13 at 14:26
  • 1
    oh sry @Thomas i thought only one so you need an behavior because you can't bind directly to SelectedItems AFAIK – WiiMaxx Jun 14 '13 at 14:26
  • @Thomas see my Edit maybe that will help you it is based on [THIS](http://stackoverflow.com/a/9880673/1993545) and if it doesn't work take a look at [THIS](http://blog.functionalfun.net/2009/02/how-to-databind-to-selecteditems.html) link – WiiMaxx Jun 14 '13 at 14:52
  • @Thomas and i forgot [THIS](http://stackoverflow.com/a/16953833/1993545) is the easiest solution i found – WiiMaxx Jun 14 '13 at 14:55
  • @WiiMaxx I finally followed the [following post](http://stackoverflow.com/questions/451748/wpf-m-v-vm-get-selected-items-from-a-listcollectionview?answertab=active#tab-top). It looks messy for me but at least it works. – Thomas Jun 14 '13 at 15:22
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.

Community
  • 1
  • 1
DRapp
  • 47,638
  • 12
  • 72
  • 142
  • 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 Jun 14 '13 at 14:11
  • @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. – DRapp Jun 14 '13 at 14:21
  • 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 Jun 14 '13 at 14:25
  • @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. – DRapp Jun 14 '13 at 14:33