简体   繁体   English

如何在WPF中为组合框项目设置值?

[英]How to set a value for a combo box item in WPF?

I started studying wpf and I tried to work with combo boxes but so far I'm having troubles. 我开始学习wpf并尝试使用组合框,但到目前为止,我遇到了麻烦。 What I wanted is to set the combo box's value to be based on the primary id of my item table and show the combo box's text based on my table's item name column. 我想要的是将组合框的值设置为基于项目表的主要ID,并根据表的项目名称列显示组合框的文本。

I have a background in php so I tried coding it there so you guys could better understand what I'm trying to do: 我在php中有背景,所以我尝试在那里进行编码,以便你们更好地了解我正在尝试做的事情:

<?php

$con = mysqli_connect("localhost","root","","inventory");

echo "<form method='POST'>";
echo "<select name='value'>";
echo "<option>Select An Item</option>";

$item_query = mysqli_query($con,"SELECT * FROM item") or mysqli_error();
while($got = mysqli_fetch_assoc($item_query))
{
$item_id = $got['item_id'];
$item_name = $got['item_name'];
echo "<option value='$item_id'>$item_name</option>";    
}

echo "</select>";
echo "<input type='submit' name='submit'/>";
echo "</form>";

if(isset($_POST['submit']))
{
    echo $_POST['value'];
}

?>

What I have done so far on wpf: 到目前为止,我在wpf上所做的工作:

InitializeComponent();
        comboBox.Items.Add("One");
        comboBox.Items.Add("Two");
        comboBox.Items.Add("Three");

That's about the only thing I have unfortunately done so far. 不幸的是,这是到目前为止我唯一要做的事情。 I haven't even connected it to my database as I first wanted to determine how would I be able to apply the values then only after that I would apply my database. 我什至没有将其连接到数据库,因为我首先想确定如何应用这些值,然后才应用数据库。

Assuming you dont have experience in MVVM pattern yet, Use DataBinding in XAML and codebehind files. 假设您还没有MVVM模式的经验,请在XAML和代码隐藏文件中使用DataBinding。

  1. Implement INotifyPropertyChanged interface on your Window class. 在Window类上实现INotifyPropertyChanged接口。 INotify... is the crucial interface that notifies the UI element whenever data that the UI Element holds is changed in code behind. INotify ...是至关重要的接口,每当UI元素保存的数据在后面的代码中发生更改时,它都会通知UI元素。

You can find code to implement INotifyPropertyChanged in below link: http://www.codeproject.com/Articles/41817/Implementing-INotifyPropertyChanged 您可以在下面的链接中找到实现INotifyPropertyChanged的代码: http : //www.codeproject.com/Articles/41817/Implementing-INotifyPropertyChanged

  1. Create a custom class Item that corresponds according to your database table structure for your combobox. 根据您的组合框的数据库表结构,创建一个与之对应的自定义类Item。 Also make your class implement INotifyPropertyChanged interface. 还要使您的类实现INotifyPropertyChanged接口。 Mandatory Call the OnPropertyChanged method from setter of the property. 强制从属性的设置器调用OnPropertyChanged方法。 Sample code as below: 示例代码如下:

     public class Item:InotifyPropertyChanged { private int id: public int Id { get{return id;} set { id=value; OnPropertyChanged(new PropertyChangedEventArgs("Id")); } } private string text; public string Text { get { return text; } set { text = value; OnPropertyChanged(new PropertyChangedEventArgs("Id")); } } 

    3. Create a container property of type ObservableCollection to hold ComboBox items for your class Window as below: 3.创建类型为ObservableCollection的容器属性,以容纳类Window的ComboBox项,如下所示:

     private ObservableCollection<Item> _items; public ObservableCollection<Item> Items { get { return _items; } set { _items = value; OnPropertyChanged(new PropertyChangedEventArgs("Items")); } } 

    Create a method that populates the Items container and call the method from Window constructor in code behind. 创建一个填充Items容器的方法,然后在后面的代码中从Window构造函数调用该方法。

  2. Finally, Change your XAML ComboBox definition as below: 最后,如下更改XAML ComboBox定义:

      <ComboBox ItemsSource="{Binding Items}" DisplayMemberPath="Text" SelectedValuePath="Id"/> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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