How ComboBox binds data in the WPF DataGrid

Source: Internet
Author: User
Tags bind wpf datagrid

In the Delphi era, I do not like to edit the data directly on the Stringgrid, and if the entire ComboBox in this Drop-down selection box, it is simply troublesome.

After using WPF, I think it should be edited directly on the Stringgrid, otherwise, you will have to drag out a lot of TextBox and ComboBox out, then the DataGrid is left only the selected role.

The text is very say, ComboBox on the trouble, how a trouble law? Binding up the data is useless!


1. Create a new list as the data source and set the window's Datacontex to this.

The code is as follows Copy Code

Public observablecollection<department> Listdepartments{set;get;}

Listdepartments = DB. Getcollection<department> ();

This. DataContext = this;

2, the DataGrid directly bound listdepartments

The code is as follows Copy Code


<datagridcomboboxcolumn width= "header=" professional group selectedvaluepath= "Name" textbinding= "{Binding Department}" Itemssource= ' {Binding path=listdepartments} ' displaymemberpath= ' Name '/>

The above approach, is the general control of the data binding method, but put it in here is not the spirit!

Then hurry to find a solution, online things a lot, but do not solve the problem, probably said:

1, ComboBox in the DataGrid there is no Datacontex, so can not bind to the TreeView.

2, ComboBox can only bind static resources in the DataGrid.

3, and said Anccerster wrong.

Go to Microsoft MSDN for a look:

To populate the Drop-down list with one of the following options, set the ComboBox ItemsSource property First:
Static resources. For more information, see StaticResource Markup extensions.
X: Static code entity. For more information, see x:static Markup extensions.
An inline collection of comboboxitem types.

Damn, the requirements are really high, only the following three situations can be: static resources, static code entities, and one is to put your list data into a comboboxitem set.

Well, that's the easiest way, the second:

To add a static entity to the resource, and then binding the resource, two steps are done:

The code is as follows Copy Code


<Window.Resources>

<collectionviewsource x:key= "departments" source= "{Binding listdepartments}"/>

</Window.Resources>

<datagridcomboboxcolumn width= "header=" professional group selectedvaluepath= "Name" textbinding= "{Binding Department}" Itemssource= ' {Binding source={staticresource departments} ' displaymemberpath= ' Name '/>

Oh, it's hard.

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.