简体   繁体   中英

C#/WPF/xaml ComboBox populate with text and value using a datatable

My Goal is to have a user select an item from a combobox and then use the associated value in a variable.

Below is the code I have created, it populates the combobox with item from a datatable, but I'm unable to figure out how to populate the combobox with the value also and apply the value to a variable for later use.

Yes.. I'm very new to C#/WPF/XAML

#MainWindow.xaml

<Window x:Class="ComboBoxFruitwValue.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">
    <Grid>
    <StackPanel Orientation="Vertical">
        <StackPanel Orientation="Horizontal">
            <Label Content="Fruit:" VerticalContentAlignment="Center" 
        VerticalAlignment="Center"></Label>
        <ComboBox x:Name="cbxFruitDt" Width="300" Margin="15,0,0,0" VerticalAlignment="Center" 
        VerticalContentAlignment="Center" SelectionChanged="cbxFruitDt_SelectionChanged" >

        </ComboBox>

        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBox x:Name="txt1" Width="200" />

        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBox x:Name="txt2" Width="200" />

        </StackPanel>

        <StackPanel Orientation="Horizontal">
            <TextBox x:Name="txt3" Width="200" />

        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBox x:Name="txt4" Width="200" />

        </StackPanel>
    </StackPanel>

    </Grid>

</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ComboBoxFruitwValue
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        fill_cbxfruit();
    }
    void fill_cbxfruit()
    {
        using (DataTable dtFruit = new DataTable("fruits"))
        {
            dtFruit.Columns.Add("ID", typeof(int));
            dtFruit.PrimaryKey = new DataColumn[] { dtFruit.Columns["ID"] };
            dtFruit.Columns.Add("FruitCode", typeof(string));
            dtFruit.Columns.Add("FruitDescription", typeof(string));
            dtFruit.Rows.Add(1, "AP", "Apple");
            dtFruit.Rows.Add(2, "OR", "Orange");
            dtFruit.Rows.Add(3, "PE", "Pear");
            dtFruit.Rows.Add(4, "MA", "Mango");
            dtFruit.Rows.Add(5, "PA", "PineApple");
            dtFruit.Rows.Add(6, "GR", "Grapes");
            dtFruit.Rows.Add(7, "CH", "Cherries");
            dtFruit.Rows.Add(8, "SB", "Strawberries");
            dtFruit.Rows.Add(9, "BA", "Bananna");

            for (int i = 0; i < dtFruit.Rows.Count; i++)
            {
             cbxFruitDt.Items.Add(dtFruit.Rows[i]["FruitDescription"]);
            }



        }
    }

    private void cbxFruitDt_SelectionChanged(object sender,        SelectionChangedEventArgs e)
    {

        txt1.Text = cbxFruitDt.SelectedIndex.ToString();
        txt2.Text = cbxFruitDt.SelectedItem.ToString();
        txt3.Text = cbxFruitDt.SelectedValuePath;
        //txt4.Text = cbxPccDt.DisplayMemberPath;
        txt4.Text = cbxFruitDt.Text;
    }
}
}

In order to bind the combobox you have to specify these attributes:

DisplayMemberPath="column name" 
SelectedValuePath="column name"

In your case the changes in xamal code are:

 <ComboBox x:Name="cbxFruitDt" Width="300" Margin="15,0,0,0" 
               VerticalAlignment="Center" 
               VerticalContentAlignment="Center"   
               SelectionChanged="cbxFruitDt_SelectionChanged" 
               ItemsSource="{Binding}" 
               DisplayMemberPath="FruitCode" 
               SelectedValuePath="FruitDescription">
    </ComboBox>

And binding code will be:

 cbxFruitDt.ItemsSource = dtFruit;
 cbxFruitDt.DisplayMemberPath = "FruitCode";
 cbxFruitDt.SelectedValuePath = "FruitDescription";
 cbxFruitDt.DataBind();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM