This is a migrated thread and some comments may be shown as answers.

Select item in declaratively bound combobox

2 Answers 140 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Jeremy
Top achievements
Rank 1
Jeremy asked on 29 Feb 2012, 05:18 AM
Hi

I am using a Telerik radcombobox in a ItemsControl like below. The itemscontrol is populated with some data, and I want the combobox to be default selected to one of the fields in the data. Why doesn't this work?


<Window.Resources>
   <CollectionViewSource x:Key="orderViewSource" />
</Window.Resources>
 
<Grid>
   <ItemsControl x:Name="rptOrders" ItemsSource="{Binding Source={StaticResource orderViewSource}}">
      <ItemsControl.ItemTemplate>
         <DataTemplate>
            <Grid>
               <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="160px" />
                  <ColumnDefinition Width="Auto" />
               </Grid.ColumnDefinitions>
               <Label Content="{Binding Path=Name}" Grid.Column="0" VerticalAlignment="Center"></Label>
               <telerik:RadComboBox x:Name="rcbOrder" Grid.Column="1" SelectedItem="{Binding Path=Order}">
                  <telerik:RadComboBoxItem Content="Extra Hot" />
                  <telerik:RadComboBoxItem Content="Hot" />
                  <telerik:RadComboBoxItem Content="Mild" />
               </telerik:RadComboBox>
            </Grid>
         </DataTemplate>
      </ItemsControl.ItemTemplate>
   </ItemsControl>
</Grid>


Data:
((CollectionViewSource)(FindResource("orderViewSource"))).Source = Data.StaffOrders;
 
public class Data
{
   public static ObservableCollection<StaffMember> StaffOrders { get { return //loaddata; } }
   public class StaffMember
   {
      public Int32 StaffID { get; set; }
      public String Name { get; set; }
      public String Email { get; set; }
      public String Order { get; set; }
   }
}


2 Answers, 1 is accepted

Sort by
0
Accepted
Yana
Telerik team
answered on 05 Mar 2012, 04:26 PM
Hi Jeremy,

You should set the binding like this:

<ItemsControl x:Name="rptOrders" ItemsSource="{Binding Source={StaticResource orderViewSource}}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="160px" />
                    <ColumnDefinition Width="220px" />
                </Grid.ColumnDefinitions>
                <Label Content="{Binding Path=Name}" Grid.Column="0" VerticalAlignment="Center"></Label>
                <telerik:RadComboBox x:Name="rcbOrder" Grid.Column="1" SelectedValue="{Binding Order}" SelectedValuePath="Content" Width="200">
                    <telerik:RadComboBoxItem Content="Extra Hot" />
                    <telerik:RadComboBoxItem Content="Hot" />
                    <telerik:RadComboBoxItem Content="Mild" />                     
                </telerik:RadComboBox>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Note how SelectedValuePath and SelectedValue properties are used.

Regards,
Yana
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Jeremy
Top achievements
Rank 1
answered on 07 Mar 2012, 02:30 AM
Perfect! Thanks :)
Tags
ComboBox
Asked by
Jeremy
Top achievements
Rank 1
Answers by
Yana
Telerik team
Jeremy
Top achievements
Rank 1
Share this question
or