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
Related
I have saved all combobox values which are added dynmaically to a collection.
No i want to retrieve the info. When I retrieve it my combobox values are not displayed in it.
We use Telerik component here
<tele:RadComboBox HorizontalAlignment="Stretch" DisplayMemberPath="content" ItemsSource="{Binding ManyValue,Mode=TwoWay}" SelectedItem="{Binding MyValue, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}">
ManyValue is a dropdown of values which has id,content
Myvalue is selected value what I have selected in combobox
ManyValue is a collection of viewmodel to combobox.
MyValue is a property of ViewModel i.e. selected item of comboobox.
This is happening correctly when I select, But when I retrieve the data,In collection Items are present but something wrong in Binding so that am not able to display it.
I do the save properly. But when I retrieve unable to load the already selected item in combobox.
Can anyone help?
Set the SelectedItem,SelectedValue and SelectedValuePath did the trick for me
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}" />
I'm very new to WPF. I'm trying to bind to a property a row in a DataGrid so that when the row's clicked the property is set. The ItemsSource that's bound to the DataGrid is an ObservableCollection of objects of type Field.
I've tried to bind to the SelectedItem attribute on the DataGrid, but the property is not being called. I'm using almost identical code to bind to the SelectedItem of a ComboBox and this is working fine. Is there a difference that I don't know about?
<ComboBox ItemsSource="{Binding RecordTypes}" SelectedItem="{Binding SelectedRecordType}" ...
<DataGrid ItemsSource="{Binding Fields}" SelectedItem="{Binding SelectedField}" ...
In my ViewModel:
private Field SelectedField
{
get
{
return _selectedField;
}
set
{
_selectedField = value;
}
}
(I will use auto properties later, it's just currently set up like this so that I could break when the property was set).
I'm not sure if it makes a difference, but the DataGrid is composed of 2 DataGridTextColumns and a DataGridTemplateColumn, which contains a checkbox.
Does anyone have any ideas? I'd really appreciate any suggestions.
To confirm, the reason that I want to listen to the click of a row is so that I can have the checkbox be checked whenever a row is clicked. If there's a better solution for this then please let me know.
You need to make it a two-way binding:
SelectedItem="{Binding SelectedField,Mode=TwoWay}"
That propagates changes in the view (user selects an item, SelectedItem changes) back to the viewmodel ("SelectedField" property).
Also, as #KevinDiTraglia pointed out, you need to make sure that the viewmodel property SelectedField is public, not private, otherwise the binding will not be able to access the getter/setter.
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.