WPF DataGrid Binding ComboBox

Source: Internet
Author: User
Tags wpf datagrid

WPF DataGrid binding ComboBox

One of the columns in the DataGrid in WPF is the ComboBox solves this problem for a lot of time, no nonsense about directly on the code

XAML Code

<datagridtemplatecolumn header= "group name" >

<DataGridTemplateColumn.CellTempLate>

<DataTemplate>

<combobox selectedvalue= "{Binding path=name}" itemsource={binding Path=selectionlist,relativesource

={relativesource Ancestortype={x:type usercontrol},mode=findancestor}}></combobox>

<DataTemplate>

<DataGridTemplateColumn.CellTempLate>

</datagridtemplatecolumn >

Xmal's code is finished. The following code is written in the background.

The. CS code is as follows:

Public observablecollection<string> SelectionList//observablecollection This is a class that needs to be referenced (using System.Collections.ObjectModel)

{

Get{return _selectionlist;}

Set{_selectionlist=value;}

}

Private observablecollection<string> _selectionlist=new observablecollection<string> (); This is an important step in the realization.

Why use ObservableCollection class, can search on the Internet by oneself

The next step should be to add a value to SelectionList.

SelectionList. ADD ("Name1");

SelectionList. ADD ("Name2");

So the ComboBox down the box has Name1 and Name2, of course SelectionList can also add background query Datatabel value, nothing but add a loop just

This is the value to add a value to the ComboBox, the last step, the value from the database to bind to the ComboBox, so that the default display

(select Name, field ... from table) query a collection of DataTable

The name of the DataGrid (its own name) Datagrid.itemsource=datatable.defaultview;

The queried name will be bound to the ComboBox.

WPF DataGrid Binding ComboBox

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.