Skip to main content
Home Forums Silverlight Programming Programming with .NET - General How to set SelectedItem in combobox?
15 replies. Latest Post by reinux on May 3, 2009.
(2)
hoangtuit
Member
26 points
43 Posts
11-17-2008 9:52 PM |
Hi all,
I have two data object:
public class Resource { public string ID { get; set; } public string Name { get; set; } public ResourceType Type { get; set; } } public class ResourceType { public string ID { get; set; } public string Name { get; set; } }
And I have a datagrid (Resource list) , when I choose an item and click on button edit, data will bind to a textbox (Resource Name) and a combobox (ResourceType).
But I can't set SelectedItem of combobox is resource type of resource item.
Sorry about my bad EN.
shamrat231
Contributor
4677 points
595 Posts
11-17-2008 11:31 PM |
Official way of setting the selected item of combo is using
Combo.SelectedIndex = 0; //set a number,0 indicates the first row.
However, you may want to show something like asp.net selectedItem.Text that you can actually match with your result and set it to true, In silverlight you have to do it in this way,
your class
public class ResourceType { public string ID { get; set; } public string Name { get; set; } }
1. Suppose the combo is populated that means you have added the data in this way
List<ResourceType> list = new List<ResourceType>();foreach(var n in e.result) {list.Add( new ResourceType() { ID = n.ID, Name= n.Name});// [placeholder] something will happen here
}Combo.ItemSource = list;
So to selcted a particular item like, suppose you want to select the item with name Shamrat, you do it this way
// [placeholder] [add this code there]
if (n.Name == "input") [in this case it has Shamrat]
{Combo.SelectedIndex = Combo.Items.Count -1;}
your item is selected.
and if this post was helpful then please 'Mark as Answer' - many thanks
Sharker Khaleed MahmudSoftware Developer(MCP,MCTS,MCPD[web])
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
silverli...
455 points
105 Posts
11-18-2008 6:08 AM |
Setting selectedIndex is same way as we doing in c# application
Just add any number of items using
Items.Add("Your Value");
then
yourcombo.selectedIndex=0;
this set index 0 as selected item
Gengis
8 points
8 Posts
11-18-2008 10:00 AM |
How about
ItemsSource="{Binding Source={StaticResource dataProvider}, Path=ResourceTypes}}" SelectedItem="{Binding Type}" ??
'Binding Type' assumes the datacontext is an instance of Resource. Of course the binding source may be different.
11-18-2008 8:45 PM |
May be my description is not obvious.
So, I have two list
List<Resource> resourcelist;
List<ResourceType> resourcetypelist;
and two windows.
Window 1: contain a datagrid (Resource List - ItemSource = resourcelist ).
Window 2: contain a textbox (Resource Name) and a combobox (ResourceType - ItemSource = resourcetypelist).
When I choose a item in window 1 and click edit button, Window 2 opened (with parameter is SelectedItem). In window 2, I can bind data to textbox, but I can set SelectedItem in combobox is resource type of item (Item chose in window 1).
Jonathan...
All-Star
24939 points
2,425 Posts
11-23-2008 7:43 AM |
Hi Hoangtuit,
We not are quite sure what is your real problem based on your description. To change the SelectedItem, please reference to shamrat231's reply. However, I guess you have difficulty on how to get the target item when click on the DataGrid. Below is a sample.
<data:DataGrid x:Name="myDataGrid" AutoGenerateColumns="False" Width="500" ItemsSource="{Binding}" RowHeight="40" MouseMove="myDataGrid_MouseMove" SelectionChanged="myDataGrid_SelectionChanged" > <data:DataGrid.Columns> <data:DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" Width="300"></data:DataGridTextColumn> <data:DataGridTextColumn Binding="{Binding LastName}" Header="tommy" Width="300"> <data:DataGridTextColumn.HeaderStyle> <Style TargetType="dataprimitives:DataGridColumnHeader"> <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate> <Button Content="{Binding}" Click="Button_Click" /> </DataTemplate> </Setter.Value> </Setter> </Style> </data:DataGridTextColumn.HeaderStyle> </data:DataGridTextColumn> <data:DataGridCheckBoxColumn Header="Male" Binding="{Binding Male}" ></data:DataGridCheckBoxColumn> <data:DataGridTextColumn Header="Age" Binding="{Binding Age}" Width="50" ></data:DataGridTextColumn> <data:DataGridTextColumn Header="Age" Binding="{Binding Age}"></data:DataGridTextColumn> </data:DataGrid.Columns> </data:DataGrid>
private void Button_Click(object sender, RoutedEventArgs e) { Button curButton = sender as Button; Person curPerson = curButton.DataContext as Person; //MessageBox.Show(Data.IndexOf(curPerson).ToString()); }
In other words, we need to get which item is clicked when you choosed. For the usage of DataGrid, please reference to this video tutorials.
GETTING STARTED WITH THE DATAGRID
INSERT, UPDATE, DELETE WITH THE DATAGRID
Please let me know, if my assumption is wrong.
Best regards,
Jonathan
manish.d...
99 points
21 Posts
11-23-2008 9:35 AM |
In edit button clik, you will get reference to datagrid item(Resource). Once you have that, you will either need to new up ResourceType based on information from Resouce or use LINQ to find ResourceType form ResourceTypeList that is bound to ComboBox and then set SelectedItem on ComboBox.
I have two blog post on ComboBox where I use similar techniques to find items in ComboBox
http://weblogs.asp.net/manishdalal/archive/2008/09/28/combobox-in-datagrid.aspx
http://weblogs.asp.net/manishdalal/archive/2008/10/22/cascading-combobox.aspx
11-23-2008 10:38 PM |
Thanks All,
I got it.
lijoputh...
108 points
51 Posts
12-22-2008 1:59 AM |
i have a similar problem.
i had already bind the combobox. i want to set the selected item. after clicking a button
but its not working.!!! :-(
i had set mycombo.SelectedItem="item1";
as the combobox have that item.but it doesnot displays that.
and the selected item is null.
can anybody help!!!!!!!!!..
silverst...
216 points
146 Posts
12-22-2008 8:24 AM |
display it with
mycombo.SelectionBoxItem="item1"
12-23-2008 12:16 AM |
hi silverstarter ,
i have tried the above code.
but its give me a error.
Property or indexer 'System.Windows.Controls.ComboBox.Selection Box Item' cannot be assigned to -- it is read only
12-23-2008 2:29 AM |
oh sorry, i have used both ways for little different applications
MyBox.SelectedItem = MyBox2.SelectedItem;
works fine for me
may use
MyBox.updatelayout() infront of the selection or check if it works when u choose a integer instead of the string with .selectedindex= ...
if all this doesnt work then may paste the codesnippet here
12-23-2008 4:10 AM |
here am posting the code. i have both id and value of the combobox item in the string. here the selectionboxitem is giving error.
here am posting the code.
i have both id and value of the combobox item in the string.
here the selectionboxitem is giving error.
{
slcmbFunctionalArea.UpdateLayout();
//slcmbArea.SelectedIndex= sResult[1];
//slcmbArea.SelectionBoxItem = sResult[2];
slcmbArea.SelectedItem = sResult[2];
}
12-23-2008 7:39 AM |
did u check if theres useful stuff in the result ?
ever checked if it works if u type in exactly a value which is part of the slcmbArea?
like
slcmbArea.SelectedItem = "aexistingitem";
realy weird cause in my app the stuff is working
SteveReconG
18 points
26 Posts
03-17-2009 11:28 AM |
This is rather absurb that I must use selectedindex vs. selecteditem
ie. I don't code in the code behind , I'm using MVVM
I have bound selectedItem with Two-Way mode, it should pick this up
SelectedItem="{Binding myItem, Mode=TwoWay}"
reinux
27 points
19 Posts
05-03-2009 2:20 PM |
I've reported this issue as a bug. If it's the same problem that I'm having, the problem is that ItemCollection.IndexOf, which SelectedItem calls, doesn't work with value types.