How to: Set the data source of the ComboBox control when ComboBox is used as an editing control for a column of the DataGrid

Source: Internet
Author: User

The DataGrid is an editable list control that can be used as an editing control for a column. For example, you can use the ComboBox control to edit a column, in this case, we need to set the ComboBox control drop-down list items first. How can we do this in Silverlight?

First, define the XAML as follows:

 

<Data: DataGrid grid. Row = "0" X: Name = "gridvariables" rowheight = "20" autogeneratecolumns = "false" borderthickness = "1, 1, 1">
<Data: DataGrid. Columns>
<Data: datagridtemplatecolumn header = "type" width = "120">
<Data: datagridtemplatecolumn. celltemplate>
<Datatemplate>
<Textblock text = "{binding type}"/>
</Datatemplate>
</Data: datagridtemplatecolumn. celltemplate>
<Data: datagridtemplatecolumn. celleditingtemplate>
<Datatemplate>
<ComboBox>
</ComboBox>
</Datatemplate>
</Data: datagridtemplatecolumn. celleditingtemplate>
</Data: datagridtemplatecolumn>
</Data: DataGrid. Columns>
</Data: DataGrid>
Second, you need to define a class and use a property of the class to improve the option data source, as shown below:

Public class workflowvariabletypes
{
Public list <workflowvariabletype> workflowvariabletypelist
{
Get
{
List <workflowvariabletype> types = new list <workflowvariabletype> ();
For (INT I = 0; I <14; I ++)
{
Types. Add (New workflowvariabletype {type = (variabletype) I });
}

Return types;
}
}
}
Then, we can declare an instance of this class through the user control resource, as follows:

<Usercontrol. Resources>
<Local: workflowvariabletypes X: Key = "workflowvariabletypes"/>
</Usercontrol. Resources>
 

Finally, the resource is used as follows:

<Data: datagridtemplatecolumn. celleditingtemplate>
<Datatemplate>
<ComboBox itemssource = "{binding workflowvariabletypelist, source = {staticresource workflowvariabletypes}">
</ComboBox>
</Datatemplate>
</Data: datagridtemplatecolumn. celleditingtemplate>
This process is similar to formatting the column data in the DataGrid, and we can also get an experience that the resource of the user control is very powerful, it can be used to reference static things (images, styles, etc.) or dynamic things ( Code , Class ).

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.