Compared with the previous beta version, the official version of silverlight2.0 has several more controls, such as ComboBox, progressbar, and passwordbox. The following is a summary of my personal data binding using ComboBox:
XAML.
Code
< Usercontrol X: Class = "Checkboxalldatagrid. comboboxtest"
Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"
Width = "400" Height = "300" Loaded = "Usercontrol_loaded" >
< Grid X: Name = "Layoutroot" Background = "White" >
< Grid. rowdefinitions >
< Rowdefinition Height = "30" />
< Rowdefinition Height = "Auto" />
</ Grid. rowdefinitions >
< Grid. columndefinitions >
< Columndefinition Width = "100" />
< Columndefinition Width = "Auto" />
</ Grid. columndefinitions >
< ComboBox X: Name = "Cbox_city" Grid. Row = "0" Grid. Column = "0" Width = "60" Horizontalalignment = "Left" > </ ComboBox >
< ComboBox X: Name = "Cbox_channel" Grid. Row = "0" Grid. Column = "1" Width = "60" Horizontalalignment = "Left" > </ ComboBox >
</ Grid >
</ Usercontrol >
CS.
Code
Using System. Collections. Generic;
Using System. LINQ;
Using System. windows;
Using System. Windows. controls;
Namespace Checkboxalldatagrid
{
Public Partial Class Comboboxtest: usercontrol
{
Public Comboboxtest ()
{
Initializecomponent ();
}
Private Void Usercontrol_loaded ( Object Sender, routedeventargs E)
{
This . Cbox_city.selectionchanged + = New Selectionchangedeventhandler (cbox_city_selectionchanged );
This . Cbox_channel.selectionchanged + = New Selectionchangedeventhandler (cbox_channel_selectionchanged );
This . Cbox_city.itemssource = Citysource ();
This . Cbox_city.displaymemberpath = " Name " ;
This . Cbox_city.updatelayout ();
This. Cbox_city.selectedindex= 0 ;
}
// City
Void Cbox_city_selectionchanged ( Object Sender, selectionchangedeventargs E)
{
If ( This . Cbox_city.selecteditem ! = Null )
{
VaR citally = This . Cbox_city.selecteditem;
Int Cityid = (City) citally). ID;
This . Cbox_channel.itemssource = (From In Channelsource () Where A. cityid = Cityid select );
This . Cbox_channel.displaymemberpath = " Channelname " ;
This . Cbox_city.updatelayout ();
This . Cbox_channel.selectedindex = 0 ;
}
}
// Channels
Void Cbox_channel_selectionchanged ( Object Sender, selectionchangedeventargs E)
{
}
Public List < City > Citysource ()
{
List < City > Cityinfo = New List < City > ();
Cityinfo. Add ( New City {ID = 1 , Name = " Beijing " });
Cityinfo. Add ( New City {ID = 2 , Name = " Fujian " });
Return Cityinfo;
}
Public List < Channel > Channelsource ()
{
List < Channel > Channelinfo = New List < Channel > ();
Channelinfo. Add ( New Channel {ID = 11 , Channelname = " Haidian District TV Station " , Cityid = 1 });
Channelinfo. Add ( New Channel {ID = 22 , Channelname = " Chaoyang TV Station " , Cityid = 1 });
Channelinfo. Add ( New Channel {ID = 33 , Channelname = " Fuzhou TV " , Cityid = 2 });
Channelinfo. Add ( New Channel {ID = 44 , Channelname = " Xiamen TV " , Cityid = 2 });
ReturnChannelinfo;
}
Public Struct City
{
Public Int Id { Get ; Set ;}
Public String Name { Get ; Set ;}
}
Public Struct Channel
{
Public Int Id { Get ; Set ;}
Public String Channelname { Get ; Set ;}
Public Int Cityid { Get ; Set ;}
}
}
}
Note: If the selectedindex attribute is specified during binding, the selectionchanged event will be triggered immediately after binding.
So if there are two or more links on the page (for example, the province and the city Fujian (cbox_pro) -- (cbox_city) Fuzhou, Xiamen) Controls
Note the following when binding a province by default:
1. Bind this. cbox_city.selectedindex = 0 to the province; bind the city to the province in the selectionchanged event.
2. Set this. cbox_city.updatelayout (); Make sure thatUielementThe layout of all sub-objects is updated correctly.