Microsoft Developer Network > Página principal de foros > Windows Presentation Foundation (WPF) > How to dynamically bind a WPF Combobox with the ValueMember and DisplayMember features?
Formular una preguntaFormular una pregunta
 

RespondidaHow to dynamically bind a WPF Combobox with the ValueMember and DisplayMember features?

  • jueves, 29 de octubre de 2009 1:18lini0103 Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    Hi,

    In Visual Studio 2005, there are properties such as ValueMember and DisplayMember for a combobox. Now I started to use WPF in my application and running out of idea , how i could achieve the same features of ValueMember and DisplayMember by dynamically binding the WPF Combobox in my codebehind with my dataset.

    Please advice on this with some sample code.

    Highly appreciate your help.

    Thanks

    Regards,

    Malini

Respuestas

Todas las respuestas

  • jueves, 29 de octubre de 2009 5:24William Han - MSFT Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     

    For binding WPF combobox to a dataset, you can bind ItemsSource to a table and point DisplayMemberPath to a column. Please see example below:

    Markup:
    <Window x:Class="BindComboBoxToDataSet.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <StackPanel>
            <ComboBox ItemsSource="{Binding Master}" DisplayMemberPath="Name" />
        </StackPanel>
    </Window>

    Code:
    using System;
    using System.Data;
    using System.Windows;

    namespace BindComboBoxToDataSet
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                DataSet ds = CreateDataSet();
                DataContext = ds;
            }

            public static DataSet CreateDataSet()
            {
                DataSet ds = new DataSet();

                // Create the parent table.
                DataTable tbl = new DataTable("Master");
                tbl.Columns.Add("ID", typeof(int));
                tbl.Columns.Add("Name");
                for (int i = 0; i < 3; ++i)
                {
                    DataRow row = tbl.NewRow();
                    row["ID"] = i;
                    row["Name"] = "Mater #" + i;
                    tbl.Rows.Add(row);
                }
                ds.Tables.Add(tbl);

                // Create the child table.
                tbl = new DataTable("Detail");
                tbl.Columns.Add("MasterID", typeof(int));
                tbl.Columns.Add("Info");
                for (int i = 0; i < 9; ++i)
                {
                    DataRow row = tbl.NewRow();
                    row["MasterID"] = i % 3;
                    row["Info"] = String.Format("Detail Info #{0} for master #{1}", (i / 3), (i % 3));
                    tbl.Rows.Add(row);
                }
                ds.Tables.Add(tbl);

                // Associate the tables.
                ds.Relations.Add("Master2Detail", ds.Tables["Master"].Columns["ID"], ds.Tables["Detail"].Columns["MasterID"]);

                return ds;
            }
        }
    }


    William
  • miércoles, 04 de noviembre de 2009 17:45Zhi-Xin YeMSFT, ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    Hi Malini,

    In WPF, the corresponding properties are SelectedValuePath and DisplayMemberPath, you can do the binding like follows for the WPF ComboBox:

     DataTable dt = new DataTable();
                dt.Columns.Add("c1", typeof(int));
                dt.Columns.Add("c2");

                for (int j = 0; j < 10; j++)
                {
                    dt.Rows.Add(j, "row" + j.ToString());
                }
     
                comboBox1.ItemsSource = dt.DefaultView;
                comboBox1.DisplayMemberPath = "c2";
                comboBox1.SelectedValuePath = "c1";

    For more information, see:

    ItemsControl.DisplayMemberPath Property
    http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.displaymemberpath.aspx

    Selector.SelectedValuePath Property
    http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selectedvaluepath.aspx


    If anything is unclear, please free to let me know.

    Have a nice day.

    Best Regards,
    Zhi-Xin



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
  • sábado, 07 de noviembre de 2009 2:23lini0103 Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    Thanks Zhi-Xin Ye and William Han - MSFT for the reply. I will try the codes given and feedback on this. Thx again.

    Malini