After hours and hours of googling, I still can't find a simple solution for binding an ObservableCollection to the selected items of a ListBox in a TwoWay Mode...
What I have is really simple : a ListBox with SelectionMode="Multiple", and an ObservableCollection<Contact> named SelectedContacts. I want this two to be bound. Of course my ListBox has ItemsSource="{Binding Contacts}" which is another ObservableCollection of Contact.
Now I really can't use a IsSelected bool on my Contact items, I just can't.
Thank you !
There is no simple solution. You can't bind SelectedItems.
The best solution is to select your Contact items into a view model object with an IsSelected property, bind to that, and then run a query against the primary OC when you need to get the selected items collection.
Since you said you can't/won't do that, the next best solution would likely be to handle SelectionChanged in your code-behind and manually update the VM collection from there.
Since SelectedItems is not a DependencyProperty you are not allowed to use Bindings
But there is a solution, take a look at this post:
http://blogs.microsoft.co.il/miziel/2014/05/02/wpf-binding-listbox-selecteditems-attached-property-vs-style/
Another option is to not keep track of the selected items in your ViewModel. Instead, pass them as a CommandParameter from the UI when you are trying to do some action.
Example:
<ListBox x:Name="MyListBox"
ItemsSource="{Binding SomeCollection}" />
<Button Command="{Binding SomeCommand}"
CommandParameters="{Binding SelectedItems, ElementName=MyListBox}" />
Related
I know how set bindings for i.e. the ItemSource or the SelectedItem of a ListBox. My Problem now is, that I have no clue how to bind the ListBox itself to a ListBox variable in my ViewModel to perform actions like ListBox.ClearSelected().
Instead of trying to clear listbox itself, try to set Selected item to NULL
Passing UI Elements to view model is only going to create many many many more issues.
<ListBox SelectedItem="{Binding Path=SelectedItemProp}"/>
VM
SelectedItemProp = null;
I've spent hours trying to figure out what is happening to my selecteditem property of my ComboBoxes and Im pretty sure I know what is causing the problem now, but I just dont understand why this is happening and if there is something I can do about it.
Basically I have a DataGrid with comboboxes in the headers that I use for filtering the DataGrid items. The DataGrid sits in a Tabcontrol and if I select items in the comboxes and switch tab and back the selected items are removed.
This happens only when the ItemsSource of the Comboboxes have the IsAsync property set to True. Otherwise everything works just like I want it to.
To give some more info about the logic:
The comboboxes get their values from a IValueConverter its the same converter for all comboboxes and I pass a ConverterParameter to tell it what to return. The collection sent to the converter is the items shown in the datagrid.
These values are refreshed (each item has selected parameters updated, the collection is not cleared and recreated) when the tab is activated.
This my xaml for one of the comboboxes:
<ComboBox SelectedItem="{Binding MadeBy}" ItemsSource="{Binding IssuesView,Converter={StaticResource DataGridFilterableValueConverter}",ConverterParameter=Madeby, IsAsync=true}" SelectionChanged="FilterComboBox_SelectionChanged"/>
Now, like i described previously, if I set the IsAsync to false the selecteditems stay.
Do I just have to live with this? We work alot with sql-databases and IsAsync really makes the interface alot more snappy. I can live without it in the comboboxes, but I tend to use it as much as I can.
I've seen that ppl have been having problems with the selecteditem and the general solution seems to be placing the SelectedItem before the ItemsSource, but this does not help.
Thanks
Erik
Edit 1:
Here is the xaml of the tabcontrol and tabitem.
<TabControl Grid.Row="1" x:Name="TabControl" SelectionChanged="TabControl_SelectionChanged">
<TabItem Header="Granskningssynpunkter" x:Name="IssueTab">
..... Lots of code here but nothing that concerns TabControl or TabItem
I would like to have a Key press of the Delete Key to fire off a DelegateCommand to actually delete the selecteditem at that point.
I am having troubles finding the right syntax to do this. I am using the INotifyPropertyChanged Implementation to bind to public propreties on my ViewModel.
Is there a way to do this? Seems like there should be.
I have an
ObservableCollection<Object> Objects
lets say with fields in each Object (i.e. name, address)
Thanks for any help
To get a command to fire from a keypress:
<ListView ItemsSource="{Binding Path=Objects}" SelectedItem="{Binding Path=SelectedObject}">
<ListView.InputBindings>
<KeyBinding Key="Delete" Command="{Binding Path=MyCommand}"></KeyBinding>
</ListView.InputBindings>
</ListView>
For your selected item, have a property in your viewModel and bind the listView's SelectedItem to it.
You could use the 'event to command' feature in the MVVM Light Toolkit. By using this, you can set the keydown event to a command that gets fired in your ViewModel, along w/ the selected item's index as your param. Within your ViewModel, assuming your observablecollection is hooked up to your ListView right, you can then remove the selected item from the collection based on the index. Make sure the updatesourcetrigger on the items property of the listview is set to "property changed".
I have a listbox which can be edited. Once an item in that listbox is edited and saved, the newly edited item is displayed in the list however its position changes. I want to be able to have that item selected or highlighted even afer its position changes. I can get its new index however I couldnt manage to find a way to have re-selected programitcally after its psoiton changes in the listbox!
Any help here would be greatly appreciated
Thanks
ListBox.SelectedIndex = newPosition;
or
ListBox.SelectedIndices.Add(newPosition);
with an optional SelectedIndices.Clear() before if you want just your item to be selected.
i assume that the itemssource is some kind of collection. why you dont use the SelectedItem property with Mode=TwoWay to select the item you want from your c# code?
<ListBox ItemsSource="{Binding Path=YourItemsCollection}"
SelectedItem="{Binding Path=MySelectedItem, Mode=TwoWay}" />
another way is to use the ICollectionView MoveCurrentTo method. all you have to do is to create a ICollectionView with CollectionViewSource.GetDefaultView(this.YourItemsCollection). if you take this way you do not need binding to SelectedItem but you have to set IsSynchronizedWithCurrentItem=true for your listbox.
I'm having a problem with a combobox. I use databinding to display the content of a list (guinodes). My UINode items in the list implement INotifyPropertyChanged and raise PropertyChanged on name changes. If I change the name of an item the combobox is updated, however combobox.Text remains the old value. Also please note that combobox.SelectedValue.ToString() contains only the type.
Databinding looks like that :
ItemsSource="{Binding ElementName=graphCanvas, Path=guinodes}"
your combobox SelectedValue or SelectedItem should be the selected UINode item, just take it and get the information you want from it. dont know why you use combobox.Text?
use this:
<ComboBox x:Name="SubCategory" ItemsSource="{Binding ElementName=graphCanvas, Path=guinodes}" DisplayMemberPath="SubCategoryName" SelectedValuePath="**SubCategoryID**" SelectedValue="{Binding SubCategoryID,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}" />
Good Luck