Select item in declaratively bound combobox

3 posts, 1 answers
  1. Jeremy
    Jeremy avatar
    96 posts
    Member since:
    Mar 2010

    Posted 28 Feb 2012 Link to this post

    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. Answer
    Yana
    Admin
    Yana avatar
    4554 posts

    Posted 05 Mar 2012 Link to this post

    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 >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Jeremy
    Jeremy avatar
    96 posts
    Member since:
    Mar 2010

    Posted 06 Mar 2012 Link to this post

    Perfect! Thanks :)
Back to Top