WPF learning-ComboBox

Source: Internet
Author: User
Today, ComboBox is used in the Olympic special protection system. Therefore, some usage methods are recorded here.
In addition, some knowledge about LINQ is also used in the process...

The following is what I wrote when I went home in the evening. It has been tested...

<Grid>
<ComboBox Height = "23" horizontalalignment = "Left" margin = "0, 11, 120 "name =" combox1 "verticalignment =" TOP "width =" "itemssource =" {binding} "selectedindex =" 0 ">
<ComboBox. itemtemplate>
<Datatemplate>
<Textblock text = "{binding Path = key}">

</Textblock>
</Datatemplate>
</ComboBox. itemtemplate>
</ComboBox>
<Button Height = "23" margin = "151,11, 101 "name =" btn_initcombobox "verticalalignment =" TOP "horizontalalignment =" Left "width =" "Click =" btn_initcombobox_click "> set ComboBox
</Button>
<ComboBox Height = "23" horizontalalignment = "Left" margin = "0, 94, 120 "name =" combox2 "verticalignment =" TOP "width =" "itemssource =" {binding} "selectedindex =" 0 ">
<ComboBox. itemtemplate>
<Datatemplate>
<Textblock text = "{binding Path = keys}"> </textblock>
</Datatemplate>
</ComboBox. itemtemplate>
</ComboBox>
<Button Height = "23" horizontalalignment = "Left" margin = "151,94, 101 "name =" button1 "verticalignment =" TOP "width =" "Click =" button#click "> button </button>
<Label Height = "28" horizontalalignment = "right" margin = "0, 11, 120 "name =" label1 "verticalignment =" TOP "width =" "> bind from idictionary </label>
<Label Height = "28" horizontalalignment = "right" margin = "0,94, 132,0 "name =" label2 "verticalignment =" TOP "width =" 120 "> anonymous objects and arraylist </label>
<ComboBox Height = "23" horizontalalignment = "Left" margin = "0, 0, 0,117 "name =" combox3 "verticalignment =" bottom "width =" 120 "itemssource =" {binding} ">
<ComboBox. itemtemplate>
<Datatemplate>
<Textblock text = "{binding Path = column1}"> </textblock>
</Datatemplate>
</ComboBox. itemtemplate>
</ComboBox>
<Button Height = "23" horizontalalignment = "Left" margin = "151,0, 0,117 "name =" button2 "verticalignment =" bottom "width =" 101 "Click =" button2_click "> button </button>
<Label Height = "28" horizontalalignment = "right" margin = "122,117, 120" name = "label3" verticalalignment = "bottom" width = ""> label </label>
<ComboBox Height = "23" horizontalalignment = "Left" margin = "0, 0, 120 "name =" combox4 "verticalignment =" bottom "width =" "itemssource =" {binding} ">
<ComboBox. itemtemplate>
<Datatemplate>
<Textblock text = "{binding Path = column11}"> </textblock>
</Datatemplate>
</ComboBox. itemtemplate>
</ComboBox>
<Button Height = "23" margin = "151,0, 101 "name =" button3 "verticalignment =" bottom "horizontalalignment =" Left "width =" "Click =" button3_click "> button </button>
</GRID>

BackgroundCode:

Private void btn_initcombobox_click (Object sender, routedeventargs E)
{
Idictionary <string, string> comboxitems = new dictionary <string, string> ();

For (INT I = 0; I <10; I ++)
Comboxitems. Add (I. tostring (), I. tostring ());
This. combobox1.datacontext = comboxitems;
}

private void button#click (Object sender, routedeventargs e)
{< br> arraylist arrlist = new arraylist ();
for (INT I = 0; I <10; I ++)
{< br> arrlist. add (New {keys = I. tostring (), values = (I + 1 ). tostring ()});
}< br> This. combobox2.datacontext = arrlist;
}

Private void button2_click (Object sender, routedeventargs E)
{
Datatable dt = new datatable ();
Datacolumn col1 = new datacolumn ("column1 ");
Datacolumn col2 = new datacolumn ("column2 ");

DT. Columns. Add (col1 );
DT. Columns. Add (col2 );

For (INT I = 0; I <10; I ++)
{
Datarow ROW = DT. newrow ();
Row [0] = I. tostring ();
Row [1] = I. tostring ();
DT. Rows. Add (ROW );
}

This. combobox3.datacontext = DT;
}

Private void button3_click (Object sender, routedeventargs E)
{
Datatable dt = new datatable ();
Datacolumn col1 = new datacolumn ("column1 ");
Datacolumn col2 = new datacolumn ("column2 ");

DT. Columns. Add (col1 );
DT. Columns. Add (col2 );

for (INT I = 0; I <10; I ++)
{< br> datarow ROW = DT. newrow ();
row [0] = I. tostring ();
row [1] = I. tostring ();
DT. rows. add (ROW);
}

Ienumerable temp = from row in DT. asenumerable ()
Where row. Field <string> ("column1 ")! = "1"
Select New {column11 = row. Field <string> ("column1"), column22 = row. Field <string> ("column2 ")};
This. combobox4.datacontext = temp;
}

There's nothing to talk about. You can see the code...

If you are free, change all the front-end bindings to the back-end.ProgramImplementation ....

Add By yanchanggang 2008-05-23 00:51:00
Bind the background program and select the template...

Datatable dt = new datatable ();
Datacolumn col1 = new datacolumn ("column1 ");
Datacolumn col2 = new datacolumn ("column2 ");

DT. Columns. Add (col1 );
DT. Columns. Add (col2 );

For (INT I = 0; I <10; I ++)
{
Datarow ROW = DT. newrow ();
Row [0] = I. tostring ();
Row [1] = I. tostring ();
DT. Rows. Add (ROW );
}

Ienumerable temp = from row in DT. asenumerable ()
Where row. Field <string> ("column1 ")! = "1"
Select New {column11 = row. Field <string> ("column1"), column22 = row. Field <string> ("column2 ")};

Datatemplate = new datatemplate ();
Frameworkelementfactory = new frameworkelementfactory (typeof (Label); // create a control Template
Binding binding = new binding ();
Binding. Path = new propertypath ("column11"); // the field to which the object is bound.
Frameworkelementfactory. setbinding (contentproperty, binding); // you can specify the property bound to the control...
Datatemplate. visualtree = frameworkelementfactory;
This. combobox5.itemtemplate = datatemplate;
This. combobox5.itemssource = temp;

Front-end:
<ComboBox Height = "23" horizontalalignment = "Left" margin = "120," name = "combobox5" verticalignment = "bottom" width = ""/>

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.