locked
ComboBox SelectedItem RRS feed

  • Question

  • Hello,
     
     I have a ComboBox inside GridView Like below. I need to get the SelectedItem of ComboBox fromViewModel.
     Thats why I have set like this. SelectedItem="{Binding ComboSelectedItem,Mode=TwoWay}"
     
     But the problem is when I select any other ComboBox the selected item is shown all previously selected ComboBox.


    <telerik:RadGridView   x:Name="RadGridView"  ItemsSource="{Binding ItemCollection, Mode=TwoWay}" SelectionMode="Extended" AutoGenerateColumns="False" >
         <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding EmpID}" />
            <telerik:GridViewColumn >
                <telerik:GridViewColumn.CellTemplate>
                  <DataTemplate>
                        <ComboBox x:Name="ComboBox" SelectedItem="{Binding ComboSelectedItem,Mode=TwoWay}" >
                        </ComboBox>
                  </DataTemplate>
                </telerik:GridViewColumn.CellTemplate>
           </telerik:GridViewColumn>
         </telerik:RadGridView.Columns>
     </telerik:RadGridView>


    Please help me.

    Thanks.

    Wednesday, May 25, 2011 1:24 PM

Answers

  • Hi,

    May be you dislike my answered subscribe to load event then reference in code.

    <ComboBox x:Name="ComboBox" SelectedItem="{Binding Source={StaticResource myViewModel},Path=ComboSelectedItem,Mode=TwoWay}" >
                        </ComboBox>

    Thursday, May 26, 2011 7:44 AM

All replies

  • Why do you have your ComboBox specified in the default cell data template? Isn't it more suited for edit mode by placing it in a edit data template?

    http://www.telerik.com/help/silverlight/telerik.windows.controls.gridview-telerik.windows.controls.gridviewcolumn-celledittemplate.html 

    Brice 

    Wednesday, May 25, 2011 1:54 PM
  • Because you placed the combobox as an item of a GridView, it's binding to its parent which is the GridView not the ViewModel as you expected.

    You need to specify such that the binding would refer back to your ViewModel. Something similar to the following will work:

    <UserControl.Resources> 
            <my:MyViewModel x:Key="myViewModel"></my:MyViewModel> 
     </UserControl.Resources>
     
         

    <telerik:RadGridView   x:Name="RadGridView"  ItemsSource="{Binding ItemCollection, Mode=TwoWay}" SelectionMode="Extended" AutoGenerateColumns="False" >
         <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding EmpID}" />
            <telerik:GridViewColumn >
                <telerik:GridViewColumn.CellTemplate>
                  <DataTemplate>
                        <ComboBox x:Name="ComboBox" SelectedItem="{Binding Source={StaticResource myViewModel},Path=ComboSelectedItem,Mode=TwoWay}" >
                        </ComboBox>
                  </DataTemplate>
                </telerik:GridViewColumn.CellTemplate>
           </telerik:GridViewColumn>
         </telerik:RadGridView.Columns>
     </telerik:RadGridView>

    Wednesday, May 25, 2011 2:07 PM
  • Hello,


    Yes, I have already tried that. I could able to get the SelectedItem to ViewModel. My issue is when i select an item in ComboBox it shows the selected item in all other combobox in GridView. Please suggest a solution.


    Thanks,

    Thursday, May 26, 2011 7:39 AM
  • Hi,

    May be you dislike my answered subscribe to load event then reference in code.

    <ComboBox x:Name="ComboBox" SelectedItem="{Binding Source={StaticResource myViewModel},Path=ComboSelectedItem,Mode=TwoWay}" >
                        </ComboBox>

    Thursday, May 26, 2011 7:44 AM
  • Yep! That was incorrect.

    Can you extend the class of object belong in the "ItemCollection" list to include a property named CombocSelectedItem to store the selected item from the combobox of each row correspondingly?

    Thursday, May 26, 2011 12:55 PM