silverlight combobox数据绑定
silverlight2.0正式版比较之前的beta版本来说又多了几个控件例:ComboBox, ProgressBar, 和 PasswordBox等.以下是个人在运用ComboBox的数据绑定过程中做了个人小结:
xaml.
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<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.
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
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;
}
//城市
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 a in ChannelSource() where a.cityid == CityID select a);
this.CBox_Channel.DisplayMemberPath = "channelName";
this.CBox_City.UpdateLayout();
this.CBox_Channel.SelectedIndex = 0;
}
}
//频道
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 = "北京" });
cityinfo.Add(new city { id = 2, name = "福建" });
return cityinfo;
}
public List<channel> ChannelSource()
{
List<channel> channelinfo = new List<channel>();
channelinfo.Add(new channel { id = 11, channelName = "海淀区电视台", cityid = 1 });
channelinfo.Add(new channel { id = 22, channelName = "朝阳区电视台", cityid = 1 });
channelinfo.Add(new channel { id = 33, channelName = "福州市电视台", cityid = 2 });
channelinfo.Add(new channel { id = 44, channelName = "厦门市电视台", cityid = 2 });
return channelinfo;
}
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; }
}
}
}
注:如果在绑定的时候就给他指定SelectedIndex属性则在绑定后马上触发SelectionChanged事件
所以在页面如果有两个或两个以上的连动(例:省份与城市 福建省(Cbox_pro) -- (Cbox_city) 福州,厦门)的控件
且要求默认选定一个省份时绑定时应注意:
1.在省份的绑定this.CBox_City.SelectedIndex = 0; 城市的绑定应写在省份的SelectionChanged事件里
2.设置this.CBox_City.UpdateLayout();确保 UIElement 的所有子对象位置都正确地进行了布局更新