I am trying to bind data with combobox but couldn't bind it.
CBFolders.ItemsSource = client.GetNewsLetterFoldersAsync("token", 12, 3427).AsDataView();
In this code I am using web service which method GetNewsLetterFolders return data table.
Please help me how I can bind this??
Return type of web service method should be a model class. If it's returning folderId & Description and both properties should member of FolderData class. Hence FolderData essentialy become table.
I consider service method GetNewsLetterFoldersAsync returns List. So combox box should be bind like this
CBFolders.ItemsSource = await client.GetNewsLetterFoldersAsync("token", 12, 3427);
<ComboBox x:Name="CBFolders" SelectedValuePath="folderId" DisplayMemberPath="Description" />
SelectedValuePath should be bind with that property which is needed while selection changes in ComboBox. DisplayMemberPath should be bind with that property which is needed to show in ComboBox.
CBFolders.SelectedItem will return selected folderId
You need to convert the datatable to List when you are binding it to ItemsSOurce property. CBFolders.ItemsSource = client.GetNewsLetterFoldersAsync("token", 12, 3427).GetList(). Hope this helps.
Related
What is the difference betweeen the following:
SelectedItem
SelectedValue
SelectedValuePath
All these dependency properties are defined in Selector class. I often confuse SelectedItem with SelectedValue , and SelectedValue with SelectedValuePath.
I would like to know the difference between them, and also when do we use them, especially SelectedValue and SelectedValuePath. Please explain their use with some simple examples.
Their names can be a bit confusing :). Here's a summary:
The SelectedItem property returns the entire object that your list is bound to. So say you've bound a list to a collection of Category objects (with each Category object having Name and ID properties). eg. ObservableCollection<Category>. The SelectedItem property will return you the currently selected Category object. For binding purposes however, this is not always what you want, as this only enables you to bind an entire Category object to the property that the list is bound to, not the value of a single property on that Category object (such as its ID property).
Therefore we have the SelectedValuePath property and the SelectedValue property as an alternative means of binding (you use them in conjunction with one another). Let's say you have a Product object, that your view is bound to (with properties for things like ProductName, Weight, etc). Let's also say you have a CategoryID property on that Product object, and you want the user to be able to select a category for the product from a list of categories. You need the ID property of the Category object to be assigned to the CategoryID property on the Product object. This is where the SelectedValuePath and the SelectedValue properties come in. You specify that the ID property on the Category object should be assigned to the property on the Product object that the list is bound to using SelectedValuePath='ID', and then bind the SelectedValue property to the property on the DataContext (ie. the Product).
The example below demonstrates this. We have a ComboBox bound to a list of Categories (via ItemsSource). We're binding the CategoryID property on the Product as the selected value (using the SelectedValue property). We're relating this to the Category's ID property via the SelectedValuePath property. And we're saying only display the Name property in the ComboBox, with the DisplayMemberPath property).
<ComboBox ItemsSource="{Binding Categories}"
SelectedValue="{Binding CategoryID, Mode=TwoWay}"
SelectedValuePath="ID"
DisplayMemberPath="Name" />
public class Category
{
public int ID { get; set; }
public string Name { get; set; }
}
public class Product
{
public int CategoryID { get; set; }
}
It's a little confusing initially, but hopefully this makes it a bit clearer... :)
Chris
To answer a little more conceptually:
SelectedValuePath defines which property (by its name) of the objects bound to the ListBox's ItemsSource will be used as the item's SelectedValue.
For example, if your ListBox is bound to a collection of Person objects, each of which has Name, Age, and Gender properties, SelectedValuePath=Name will cause the value of the selected Person's Name property to be returned in SelectedValue.
Note that if you override the ListBox's ControlTemplate (or apply a Style) that specifies what property should display, SelectedValuePath cannot be used.
SelectedItem, meanwhile, returns the entire Person object currently selected.
(Here's a further example from MSDN, using TreeView)
Update: As #Joe pointed out, the DisplayMemberPath property is unrelated to the Selected* properties. Its proper description follows:
Note that these values are distinct from DisplayMemberPath (which is defined on ItemsControl, not Selector), but that property has similar behavior to SelectedValuePath: in the absence of a style/template, it identifies which property of the object bound to item should be used as its string representation.
SelectedItem and SelectedValue are an object.
and SelectedValuePath is a string.
for example using the ListBox:
Below listbox1.SelectedValue becomes a string value.
string value = listbox1.SelectedValue;
if you say give me listbox1.SelectedItem it will give you the entire object.
ListItem item = listbox1.SelectedItem;
string value = item.value;
inspired by this question I have written a blog along with the code snippet here. Below are some of the excerpts from the blog
SelectedItem – Selected Item helps to bind the actual value from the DataSource which will be displayed. This is of type object and we can bind any type derived from object type with this property. Since we will be using the MVVM binding for our combo boxes in that case this is the property which we can use to notify VM that item has been selected.
SelectedValue and SelectedValuePath – These are the two most confusing and misinterpreted properties for combobox. But these properties come to rescue when we want to bind our combobox with the value from already created object. Please check my last scenario in the following list to get a brief idea about the properties.
Every control that uses Collections to store data have SelectedValue, SelectedItem property. Examples of these controls are ListBox, Dropdown, RadioButtonList, CheckBoxList.
To be more specific if you literally want to retrieve Text of Selected Item then you can write:
ListBox1.SelectedItem.Text;
Your ListBox1 can also return Text using SelectedValue property if value has set to that before. But above is more effective way to get text.
Now, the value is something that is not visible to user but it is used mostly to store in database. We don't insert Text of ListBox1, however we can insert it also, but we used to insert value of selected item. To get value we can use
ListBox1.SelectedValue
Source
I have populated the comboBox with the Data from SQLite
Here's how I did it.
conn.CreateTable<AccountName>();
var query = conn.Table<AccountName>();
myComboBox.ItemsSource = query.ToList();
So when I try to retrieve the selected ComboBox Item to a TextBlock, I get the output as
'Apps.Models.AccountName'
How can I retrieve the values?
Look for SelectedValue, SelectedItem Gets currently selected item in the ComboBox. Use either of these.
comboBox1.SelectedValue;
or
comboBox1.GetItemText(comboBox1.SelectedItem);
I finally found the answer by using this code.
Demo.Text = ((AccountName)myComboBox.SelectedItem).AccName;
So basically, I have an entity, I've used a LINQ query to get some items and bound that to a combobox.
What I'm trying to do is have the entity update when the item in the combobox is changed without doing a manual update in the Combobox.SelectedIndex/SelectedValue changed event. I'm just wondering if there is a better way?
Pseudo code:
SomeEntity se = new SomeEntity();
var q = from x in se select x;
cbComboBox.Datasource = q.ToList();
cbComboBox.DisplayMember = "SomeValue";
cbComboBox.ValueMember = "SomeValueID";
cbComboBox.SelectedValue = se.SomeValueID;
So the combobox shows the correct value I want. That's fine.
Here's the thing, when the user changes the combobox, I want se.SomeValueID to reflect that change.
I know I can set se.SomeValueID in the combobox SelectedValue event, but is there a better way? Where se.SomeValueID automagically gets updated?
Assuming your question is about Windows Forms. To bind SelectedValue to an object's property you can add custom binding:
cbComboBox.DataBindings.Add("SelectedValue", se, "SomeValueID");
That binding is two-way and there's no need in cbComboBox.SelectedValue = se.SomeValueID; line.
You should achieve this using Bindings defined in XAML instead of C# code behind code, here is an example:
<ComboBox ItemsSource="{Binding SomeList}"
DisplayMemberPath="SomeField"
SelectedItem="{Binding CurrentItem, Mode=TwoWay}"/>
In this way you can have in your viewmodel class the list of entities (SomeList) and an object of the same type (CurrentItem) that will be updated every time the user change the selected item in the combobox. If you don't need the entire object, you can bind the selected value to a property exposed on your viewmodel with the same time of the id.
Hope this helps.
I have a Combobox, in which I would like its items to be the column data that is located on a DataGrid. Is there anyway to set the Combobox itemsource to be a specific column of a DataGrid?
Right now I'm iterating each row of the DataGrid, getting the field's data and adding them to the Combobox, but that means that I would have to clear all the items and reiterate everytime the DataGrid is modified.
You can set ItemsSource and DisplayMemberPath properties:
comboBox1.ItemsSource = dataGrid1.ItemsSource;
comboBox1.DisplayMemberPath = "ColumnName";
I Think you'are taking the wrong approach. Your data grid must be bound to a collection of object. I guess you could just build another collection by extracting the desired fields (for example with linQ) and expose this new collection to your view such that you can bind your combobox.
I you want to keep this second collection updated, make your first main collection an ObservableCollection such taht your can subscribe to CollectionChanged Event. In the event handler, just manage the add and remove in your combobox source collection.
I am working on a project for my college where I need to bind data from database into the combobox. I need to store the roll no / enrollment no in the "value" field of combobox and name of the student in the "text" property of the combobox. How can I do that?>???
Please reply ASAP....
You will need to set the DataSource of the combobox to your datasource. Then the ValueMember for the Roll No and the DisplayMember for the name of the student.
e.g
cboStudents.DataSource = dataSet1.Tables["Students"];
cboStudents.ValueMember = "RollNumber";
cboStudents.DisplayMember = "StudentName";
The two complex-bound controls you've most likely encountered are the ComboBox and the Listbox. To complex-bind one of these controls, you need to set the DataSource (where values originate), the DisplayMember (the name of the column of data that supplies the visible list items), and the ValueMember (the name of the column of data that supplies the possible control values).
combobox.DataSource = dataTable
combobox.ValueMember = "id"
combobox.DisplayMember = "name"