Skip to main content

Microsoft Silverlight

Answered Question How to set SelectedItem in combobox?RSS Feed

(2)

hoangtuit
hoangtuit

Member

Member

26 points

43 Posts

How to set SelectedItem in combobox?

 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
shamrat231

Contributor

Contributor

4677 points

595 Posts

Answered Question

Re: How to set SelectedItem in combobox?

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. Smile

and if this post was helpful then please 'Mark as Answer' - many thanks

Sharker Khaleed Mahmud
Software 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.

 

 

 

Dhaka, Bangladesh
LinkedIn :: SL Profile :: Blog

silverlight_kid
silverli...

Member

Member

455 points

105 Posts

Re: How to set SelectedItem in combobox?

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

S.Siva Sankar
Software Engineer
Protechsoft

Gengis
Gengis

Member

Member

8 points

8 Posts

Re: Re: How to set SelectedItem in combobox?

 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.

hoangtuit
hoangtuit

Member

Member

26 points

43 Posts

Re: How to set SelectedItem in combobox?

 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).

 Sorry about my bad EN.

Jonathan Shen – MSFT
Jonathan...

All-Star

All-Star

24939 points

2,425 Posts

Microsoft
Answered Question

Re: How to set SelectedItem in combobox?

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

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

manish.dalal
manish.d...

Member

Member

99 points

21 Posts

Re: How to set SelectedItem in combobox?

Hi Hoangtuit,

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

 

 

Manish Dalal
http://feeds2.feedburner.com/manishdalal

hoangtuit
hoangtuit

Member

Member

26 points

43 Posts

Re: How to set SelectedItem in combobox?

 Thanks All,

 I got it.

lijoputhuval
lijoputh...

Member

Member

108 points

51 Posts

Re: How to set SelectedItem in combobox?

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!!!!!!!!!..

 

with Regards
Lijo

silverstarter
silverst...

Member

Member

216 points

146 Posts

Re: How to set SelectedItem in combobox?

display it with

mycombo.SelectionBoxItem="item1"

lijoputhuval
lijoputh...

Member

Member

108 points

51 Posts

Re: Re: How to set SelectedItem in combobox?

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

 

with Regards
Lijo

silverstarter
silverst...

Member

Member

216 points

146 Posts

Re: Re: How to set SelectedItem in combobox?

 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

lijoputhuval
lijoputh...

Member

Member

108 points

51 Posts

Re: Re: Re: How to set SelectedItem in combobox?

here am posting the code.

i have both id and value of the combobox item in the string.

here the selectionboxitem is giving error.

void AreaServiceSoapClient_GetCompleted(object sender, MyProject.Silverlight.AreaService.GetCompletedEventArgs e)

{

 if (e.Error == null)

    {

     AreaService.
ArrayOfString sResult = e.Result

     slcmbFunctionalArea.UpdateLayout();

     //slcmbArea.SelectedIndex= sResult[1];

    //slcmbArea.SelectionBoxItem = sResult[2];

    slcmbArea.SelectedItem = sResult[2];

}

with Regards
Lijo

silverstarter
silverst...

Member

Member

216 points

146 Posts

Re: Re: Re: How to set SelectedItem in combobox?

 

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
SteveReconG

Member

Member

18 points

26 Posts

Re: Re: Re: Re: How to set SelectedItem in combobox?

 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
reinux

Member

Member

27 points

19 Posts

Re: Re: Re: Re: How to set SelectedItem in combobox?

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.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities