Combobox SelectedItem is not retrieved properly - c#

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

Related

Retrieving previously saved combobox items from wpf datagrid

I am new to WPF. below is the XAML code.
<dg:DataGrid.Columns>
<dg:DataGridTemplateColumn Header="Cars" MinWidth="70" Width="70">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="cboCars" MinWidth="70" ItemsSource="{Binding Path=Cars}" Width="70">
<ComboBoxItem>BMW</ComboBoxItem>
<ComboBoxItem>Benz</ComboBoxItem>
<ComboBoxItem>Audi</ComboBoxItem>
</ComboBox>
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
So each row contains a combo box and it contains all the 3 car names are there as the combo box item and user can choose a car from the drop down and save it. But when taking back the value from the Database, I can't display the exact car in the grid row as the previously saved item.Suppose now if i choose "Audi" and save the form and later on when i tried to retrive the saved data, the combobox will be displaying nothing!!! How to show the exact car in the grid ? Please help me.
Have a look at selection. You can define a property in your view model and bind it to the SelectedItem / SelectedValue of your combobox. After loading data you can set this property and this item is selected in your combobox.
See here: https://stackoverflow.com/a/23168586/8002376

WPF - Editable combobox text overwritten by source on load

I have an editable combobox that also has an item source which is effectively used to prompt users with some standard entries for that box whilst giving them the freedom to enter custom entries if they wish. So, everything works fine accept when I bind to a property with a value (which is not one off the list) it sets the comboboxes text to the first item in the item source. Just to be clear, the following steps demonstrate the issue.
I open the View with a clean Model (all the properties are null/default values
I enter a custom value into the editable combobox
I save the Model
I close the View
I re-open the View with the previously saved Model
The custom value which I saved should be displayed in the editable combobox, however it is overwritten by the first value in the item source list
Note: The value in the Model property is not changed, this issue is purely visual.
So, how can I stop the itemsource overwriting custom entries?
Here is the combo box XAML:
<ComboBox IsEditable="True" ItemsSource="{Binding Contact_Types, Mode=OneTime, Source={StaticResource Resources}}" Text="{Binding Model.type, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />

Adding item in empty observable collection which bind to combobox lead to auto select this item in combobox, but selected value is still null

I have this throuble:
There observable collection, which bind to combobox. When collection contains any items, all works fine, I add items in collection, then choose him in combobox, selected value is not null.
I have items control,which contains any component controls which reflect any entity. I may select parameter this entity in combobox which bind to observable collection.
If I add first new entity in content control and collection is empty, then add item in collection - this item automaticaly select in combobox, but combobox selected value is null. It turns out, this situation: item select in combobox, but combobox selected value is null. If i select this item manualy, selected value is set to this item and work further аштую I don't understand how it may be.
There is my code:
this is collection
public ObservableCollection<AnyType> AnyTemplates { get; set; }
this is xaml code combobox:
<ComboBox Name="cmbKeyA"
Width="100"
SelectedValue="{Binding Path=KeyAName}"
ItemsSource="{Binding Path=DataContext.KeyTemplates, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
DisplayMemberPath="Name"
SelectedValuePath="Name"/>
I am not quite sure I understand your problem, but from what I do understand I would use either a Dependency Property or a Property that implements the INotifyPropertyChanged event to bind to SelectedItem of the ComboBox. Then when I wanted to add an Item to the ObservableCollection (assuming you want it to be selected), I would both add it to the collection and set my new Property equal to it.

Combobox DataBinding

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

Silverlight ItemsControl and ComboBox

Im cleaning up this question and going to put in an example that makes sense to me..hopefully for you too...
so lets say i have an Items control. This control is bound to an observable collection of NBA Basketball teams (Lakers, Heat, Knicks...ect). In this observable collection of NBATeams I have a property of FavoritePlayer. I have a datatemplate that lays out how this is going to look inside the Items Control. I have a textbox which displays the team name. (this could be/should be read only..its not now) but it displays the team name.. LA Lakers. the second item in my items control is a combobox. this combo box will ultimately display my favorite player on each team.
The itemssource on the combobox is from a lookup value i have. It displays all the people on the team and the displaymemberpath on this combobox is "DisplayText"...I need my selected item on this combobox to be shown. so if my favorite player is Kobe Bryant the combo box should display this.
<telerik:RadComboBox Grid.Row="0" Grid.Column="9" Width="150" EmptyText="--Select Player--"
ItemsSource="{Binding PlayerList}"
SelectedItem="{Binding FavoritePlayer, Mode=TwoWay}"
DisplayMemberPath="DisplayText" HorizontalAlignment="Left"></telerik:RadComboBox>
I am used to just having textblocks in a listbox and then when an item is selected from within that listbox i use that selected item to bind my comboboxes that live outside of my listbox. Now i actually have the combobox in the listbox(ItemsControl now). So Im wondering the best way to bind the combobox from within an ItemsControl
I initially was just having issues getting the players to show up in the combobox..but I solved that by doing the code below.
I found my first issue of just binding the items source to the combo box from within an items control.
I had to set the bindings to look like this.
ItemsSource="{Binding DataContext.PlayerList, ElementName=ItemsControlNBATeams}"
so i had to set the name of the items control and use that in the element name and set the path = to DataContext.PlayerList. this now brings up the items in my combo box..what it doesnt do is set the selecteditem of my combo box.
Not very clear about your you are asking but does this answer your question
<ListBox ItemsSource="{Binding}" x:Name="ListBox1"></ListBox>
<TextBox Text="{Binding SelectedItem, ElementName=ListBox1}"> </TextBox>

Categories

Resources