locked
Binding to ComboBox RRS feed

  • Question

  • How can we Bind a Combobox with these two things.

    1.) Bind ComboBox with an List that has Custom Object
    2.) Select the Item in that ComboBox with an item that was selected from a DB

    I have seen examples where one or the other is shown. But not both. How can this been done?

    Thanks!!

    Friday, June 9, 2017 11:12 PM

Answers

  • The answer to both questions is yes.  The following is the XAML I use in a project:

                    <ComboBox Grid.Row="1" ItemsSource="{Binding theTrailers}" SelectedItem="{Binding SelectedMovieTrailer}">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" >
                                    <Image Height="24"  Margin="0,0,20,0" Source="/MovieBrowserDB;component/Images/Movies-icon.png"></Image>
                                    <TextBlock Text="{Binding }"></TextBlock>
                                </StackPanel>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>
    

    Now the ItemsSource is a List of objects (custom).  The SelectedItem (SelectedMovieTrailer) is a property of that custom object.  The ItemTemplate gives you the chance to define what items in the ComboBox look like (Binding just presents the ToString() of the custom object).

    If you set the SelectedMovieTrailer it will use that object as the selected object.


    Lloyd Sheen

    • Proposed as answer by Bob Ding Monday, June 12, 2017 9:16 AM
    • Marked as answer by r3nv Wednesday, June 14, 2017 1:17 PM
    Saturday, June 10, 2017 1:07 PM

All replies

  • The answer to both questions is yes.  The following is the XAML I use in a project:

                    <ComboBox Grid.Row="1" ItemsSource="{Binding theTrailers}" SelectedItem="{Binding SelectedMovieTrailer}">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" >
                                    <Image Height="24"  Margin="0,0,20,0" Source="/MovieBrowserDB;component/Images/Movies-icon.png"></Image>
                                    <TextBlock Text="{Binding }"></TextBlock>
                                </StackPanel>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>
    

    Now the ItemsSource is a List of objects (custom).  The SelectedItem (SelectedMovieTrailer) is a property of that custom object.  The ItemTemplate gives you the chance to define what items in the ComboBox look like (Binding just presents the ToString() of the custom object).

    If you set the SelectedMovieTrailer it will use that object as the selected object.


    Lloyd Sheen

    • Proposed as answer by Bob Ding Monday, June 12, 2017 9:16 AM
    • Marked as answer by r3nv Wednesday, June 14, 2017 1:17 PM
    Saturday, June 10, 2017 1:07 PM
  • Hi,

    There is a sample for your reference.

    public partial class sample1 : Window
        {
            public sample1()
            {
                InitializeComponent();
            }
    
        }
        static class MyViewModel
        {
            private static Category selectedCategoryItem = new Category() { ID = 3, Name = "IWatch" };
            public static Category SelectedCategoryItem
            {
                get { return selectedCategoryItem; }
                set { selectedCategoryItem = value; }
            }
            public static List<Category> getCategoryList()
            {
                List<Category> categoryList = new List<Category>();
                categoryList.Add(new Category() { ID = 1, Name = "Iphone" });
                categoryList.Add(new Category() { ID = 2, Name = "Ipad" });
                categoryList.Add(SelectedCategoryItem);
                categoryList.Add(new Category() { ID = 4, Name = "Ipod" });
                categoryList.Add(new Category() { ID = 5, Name = "MacBook" });
                return categoryList;
            }       
        }
        class Category : DependencyObject
        {
            public static readonly DependencyProperty IDProperty = DependencyProperty.Register("ID", typeof(int), typeof(Category), null);
            public int ID
            {
                get
                {
                    return (int)GetValue(IDProperty);
                }
                set
                {
                    SetValue(IDProperty, value);
                }
            }
            public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Category), null);
            public string Name
            {
                get
                {
                    return (string)GetValue(NameProperty);
                }
                set
                {
                    SetValue(NameProperty, value);
                }
            }
        }
       <Window.Resources>
            <ObjectDataProvider x:Key="CategoryDataProvider"  ObjectType="{x:Type local:MyViewModel}" MethodName="getCategoryList"></ObjectDataProvider>
        </Window.Resources>
        <Grid> 
    <ComboBox x:Name="comboBox5" Height="30" Margin="10" SelectedItem="{Binding Source={x:Static local:MyViewModel.SelectedCategoryItem}, Mode=OneWay}" DisplayMemberPath="Name"  ItemsSource="{Binding Source={StaticResource CategoryDataProvider}}"></ComboBox>
        </Grid>

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 12, 2017 9:25 AM