C#/ WPF / xaml ComboBox使用数据表填充文本和值

时间:2016-01-21 16:02:48

标签: c# wpf xaml combobox

我的目标是让用户从组合框中选择一个项目,然后在变量中使用相关值。

下面是我创建的代码,它使用数据表中的项填充组合框,但是我无法弄清楚如何使用值填充组合框并将值应用于变量以供以后使用。 / p>

是的..我是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;
    }
}
}

1 个答案:

答案 0 :(得分:2)

为了绑定组合框,您必须指定以下属性:

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

在您的情况下,xamal代码中的更改为:

 <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>

绑定代码将是:

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